Imagine you run into a 11-year-old kid in your neighborhood. So active running about, with so much potential, but so in need of careful attention.

And so are Node.js and Redis. They have matured over time and are powering various services. But they also require careful attention from the community.

Enough about the 11-year-old analogy. Let’s review some basic stuff around Redis.

Facts about Redis

  • in-memory data structure store, meaning efficient memory management is everything!
  • config file is located at /usr/local/etc/redis.conf (on Mac)
  • the value of collections is two-fold:
    • atomic data structure prevents race condition
    • ability to leverage external collections helps you focus on business logic
  • stay away from O(N) Operations, including:
    • KEYS (use SCAN instead)
    • Delete Collections
    • Get All Collections (make smaller collections instead)
  • 4 common use cases
    • auth token (strings or hash)
    • ranking board (sorted set)
    • user API limit
    • job queues (list)

Data structures

String (key:value)

  set key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]
  get key
  expire key seconds
  incr key
  decr key
  incrby key increment
  del key [key ...]
  mset key value [key value ...]
  mget key [key ...]

List (linked list)

  • can be taken at constant length in constant time
  • use when inserting from either end
  lpush key element [element ...]
  rpush key element [element ...]
  lrange key start stop

Set (unique values)

  sadd key member [member ...]
  smembers key
  sinter key [key ...]> sadd julia_hobbies writing running tennis eating
  (integer) 4> sadd alex_hobbies running movies driving eating
  (integer) 4> smembers julia_hobbies
  1) "eating"
  2) "tennis"
  3) "running"
  4) "writing"> sinter julia_hobbies alex_hobbies
  1) "eating"
  2) "running"

Sorted Set (preserve order)

  • score field is added
  zadd key [NX|XX] [CH] [INCR] score member [score member ...]
  zrange key start stop [WITHSCORES]
  zrevrange key start stop [WITHSCORES]> zadd holidays 505 "Children's day" 606 "Memorial Day" 815 "Liberation Day" 930 "Chuseok" 1003 "National Foundation Day" 1009 "Hangeul Day" 1225 "Christmas"

  // Display values by month in ascending order> zrange holidays 0 -1
  1) "Children's day"
  2) "Memorial Day"
  3) "Liberation Day"
  4) "Chuseok"
  5) "National Foundation Day"
  6) "Hangeul Day"
  7) "Christmas"

  // National holidays before August> zrangebyscore holidays -inf 800
  1) "Children's day"
  2) "Memorial Day"

  // National holidays between August and October> zrangebyscore holidays 800 1000
  1) "Liberation Day"
  2) "Chuseok"


  hset key field value [field value ...]
  hget key field
  hgetall key> hset week:1 msg1 "Nice work!" msg2 "Keep up the good work!" msg3 "Good enough"> hgetall week:1
  1) "msg1"
  2) "Nice work!"
  3) "msg2"
  4) "Keep up the good work!"
  5) "msg3"
  6) "Good enough"


  • executes a group of commands in a single step> multi
  OK> lpush clist a
  QUEUED> lpush clist b
  QUEUED> lrange clist 0 -1
  QUEUED> exec
  1) (integer) 1
  2) (integer) 2
  3) 1) "b"
     2) "a"


  • flushes all previously queued commands in a transaction and restores the connection state to normal
  • say you have an ever-growing wishlist, and before executing it, a second thought occurs to you, and you change your mind:> multi
  OK> lpush wishlist "new bike"
  QUEUED> lpush wishlist "chocolates"
  QUEUED> lpush wishlist "new notebook"
  QUEUED> discard