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)
    • FLUSHALL, FLUSHDB
    • 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 ...]
  127.0.0.1:6379> sadd julia_hobbies writing running tennis eating
  (integer) 4
  127.0.0.1:6379> sadd alex_hobbies running movies driving eating
  (integer) 4

  127.0.0.1:6379> smembers julia_hobbies
  1) "eating"
  2) "tennis"
  3) "running"
  4) "writing"

  127.0.0.1:6379> 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]
  127.0.0.1:6379> 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
  127.0.0.1:6379> 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
  127.0.0.1:6379> zrangebyscore holidays -inf 800
  1) "Children's day"
  2) "Memorial Day"

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

Hash

  hset key field value [field value ...]
  hget key field
  hgetall key

  127.0.0.1:6379> hset week:1 msg1 "Nice work!" msg2 "Keep up the good work!" msg3 "Good enough"

  127.0.0.1:6379> hgetall week:1
  1) "msg1"
  2) "Nice work!"
  3) "msg2"
  4) "Keep up the good work!"
  5) "msg3"
  6) "Good enough"

Multi

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

Discard

  • 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:
  127.0.0.1:6379> multi
  OK
  127.0.0.1:6379> lpush wishlist "new bike"
  QUEUED
  127.0.0.1:6379> lpush wishlist "chocolates"
  QUEUED
  127.0.0.1:6379> lpush wishlist "new notebook"
  QUEUED
  127.0.0.1:6379> discard
  OK


References