Redis: Swiss Army Knife

Redis: Swiss Army Knife

An overview of Redis, the Key-Value store with smarts and speed; includes recipes for making interesting solutions and data structures out of Redis built-in types

E88c3a272a15086332786cda54bacacc?s=128

matt swanson

January 25, 2012
Tweet

Transcript

  1. 5.

    Basics SET key value GET key Only ~120 more commands

    to learn! Best Practice for keys: “users:123:orders”
  2. 8.

    Lists LPUSH key value LPOP key LRANGE key start stop

    LLEN key Protip: Swap ‘R’ for ‘L’ and more...
  3. 9.

    Lists redis> LPUSH mylist “hi” (integer) 1 redis> LPUSH mylist

    “bye” (integer) 2 redis> LRANGE mylist 0 -1 1) “hi” 2) “bye” Length after push whole list
  4. 10.

    List Recipes LPUSH + LPOP = Stack LPUSH + RPOP

    = Queue LPUSH + LTRIM = LPUSH + BRPOP = Capped Collection Realtime Message Queue
  5. 12.

    Sets redis> SADD myset “hello” (integer) 1 redis> SADD myset

    “hello” (integer) 1 redis> SMEMBERS myset 1) “hello” Unique items only
  6. 13.

    Set Recipes SADD = Tagging SPOP = Random item Sorted

    Sets = Set w/ score More advanced... SADD + SINTER = Social Graph
  7. 15.

    Hashes redis> HSET user:1 name matt (integer) 1 redis> HGET

    user:1 name “matt” Kind of boring - but this is how you do traditional “objects”
  8. 16.

    Expiry redis> SET foo bar redis> EXPIRE foo 2 **

    Wait 2 seconds ** redis> GET foo (nil) EXPIRE key seconds
  9. 17.

    Expiry Recipes Rate Limiting SET + EXPIRE = Sessions INC

    + EXPIRE = SET + EXPIRE = Invalidate Cache