Database Reference
In-Depth Information
Most Recent Event Tracking
The inclusion of a scripting language directly in the Redis engine makes
it easy to do a few things that are otherwise difficult to get right. One of
those things is “Most Recent Event Tracking.” This is often used for
things like attribution modeling where some target event is correlated
against the most recent previous event.
Doing this in most key-value stores usually involves simply assuming
that events are being processed in time “ordered enough” such that it is
unlikely that any two events for a given user will get “out of order.” In
that case, a simple SET operation suffices.
However, in many cases, the ordering assumption does not hold or
more complicated arrangements are needed. For example, it may be
that some event types are more important than others, so even if an
event occurs later, it should not become the “most recent” if it is a lesser
event. In a normal key-value system, this requires multiple round-trips
to the server. First, the current value is obtained so the update logic can
be applied. If the update succeeds, a second round-trip updates the
event.
Using Lua scripting, this update process can be done entirely on the
server side. Assuming that the events are JSON objects with a ts field
representing the timestamp of the event, an update script would look
something like this:
if redis.call("EXISTS",KEYS[1]) == 1 then
local current = tonumber( cjson.decode(
redis.call("GET",KEYS[1]) )["ts"] )
local new =
tonumber(cjson.decode(ARGV[1])["ts"])
if(new >= current) then
redis.call("SET",KEYS[1],ARGV[1])
return 1
else
return 0
Search WWH ::




Custom Search