Skip to content

Instantly share code, notes, and snippets.

@forkfork
Last active May 27, 2020 17:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save forkfork/bde8a0f1f1d4a1d588b90318d6d9bcd3 to your computer and use it in GitHub Desktop.
Save forkfork/bde8a0f1f1d4a1d588b90318d6d9bcd3 to your computer and use it in GitHub Desktop.
Redis Streams

XADD streamname [MAXLEN items] * field value [field value ...]

Adds a set of fields with values to the specified stream. If MAXLEN items are exceeded, items are removed from the beginning of the stream to bring the length down to MAXLEN. The "*" field is to autogenerate an item ID, but can be overriden.

Return value

Simple string reply: epoch time (in milliseconds) followed by a .N (for differentiating multiple events on the same millisecond)

redis) XADD clicks * xloc 100 yloc 200 button left
1506761808036.0
redis) XADD clicks * xloc 150 yloc 30 button right
1506762349280.0
redis) XADD clicks MAXLEN 2 * xloc 150 yloc 30 button right
1506766866417.0

XREAD [BLOCK milliseconds] [COUNT items] STREAMS streamname1 streamname2 ... index1 index2 ...

Returns up to COUNT items from the specified stream(s), starting at the specified indexes. To read from the beginning of a stream, 0.0 can be passed in. To read from the end of the stream, $ can be passed as an index.

If a BLOCK argument is passed, the read will wait for the specified amount of milliseconds, returning either when the stream is written to, or when the timeout is reached. Items are returned by their ID, which is typically a millisecond timestamp with a second field for deduplication.

Return value

An array of streams, each containing an array of items, each containing an array of fieldname/value pairs.

redis) XREAD STREAMS clicks 0.0
1) 1) "clicks"
   2) 1) 1) 1506761808036.0
         2) 1) "xloc"
            2) "100"
            3) "yloc"
            4) "200"
            5) "button"
            6) "left"
      2) 1) 1506762349280.0
         2) 1) "xloc"
            2) "150"
            3) "yloc"
            4) "30"
            5) "button"
            6) "right"
redis) XREAD STREAMS clicks 1506761808036.0
1) 1) "clicks"
   2) 1) 1) 1506762349280.0
         2) 1) "xloc"
            2) "150"
            3) "yloc"
            4) "30"
            5) "button"
            6) "right"

XRANGE streamname start end [COUNT items]

Returns the COUNT items in the stream between IDs start and end (including IDs equal to those values). Minimum and maximum possible ids can be indicated by using '-' and '+'.

Return value

An array of items, each containing an array of fieldname/value pairs.

redis) XRANGE clicks - +
1) 1) 1506761808036.0
   2) 1) "xloc"
      2) "100"
      3) "yloc"
      4) "200"
      5) "button"
      6) "left"
2) 1) 1506762349280.0
   2) 1) "xloc"
      2) "150"
      3) "yloc"
      4) "30"
      5) "button"
      6) "right"
redis) XRANGE clicks 1506762349280.0 +
2) 1) 1506762349280.0
   2) 1) "xloc"
      2) "150"
      3) "yloc"
      4) "30"
      5) "button"
      6) "right"

XLEN streamname

Returns the number of items in a stream.

redis) XLEN clicks
(integer) 3

Why Streams

Redis lists:

  • Typically only have one consumer (who pops the values off of the list)
  • Has at-most-once delivery

Redis pub/sub:

  • Has CPU and memory growth corresponding to the number of consumers
  • Has at-most-once delivery

Redis streams:

  • Should scale extremely well as the number of consumers increases
  • Has at-least-once delivery

NB: Previously the concept of Streams has typically been implemented on top of sorted sets using heavy client logic and/or Lua scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment