Redis

Basics

  • Fast, in-memory cache
    • client server
    • through TCP or Unix socket
    • handles simultaneous client - a set number of maximum (default in 2.6 = 10K)
    • "Redis is an in-memory but persistent on disk database, so it represents a different trade off where very high write and read speed is achieved with the limitation of data sets that can't be larger than memory." https://diigo.com/0cda7t
    • Security - INSECURE by design, should not be allowed connection from untrusted party
  • Memory usage
    • should set max memory a sane value - otherwise will be killed by OS
  • Key-value store
  • Features
    • pipelining - multiple commands at once to save round trip
    • pub/sub - fast publish / subscribe messaging system
    • lua scripting
      • native lua debugger
    • memory optimization
    • expires - TTL
    • as LRU cache - configure Redis as a cache with fixed amount of memory and auto eviction of keys
    • transaction - possible to group commands as a single transaction
    • mass insertion
    • partitioning - distribute data among multiple instances
    • distributed locks - implement a distributed lock manager
    • keyspace notifications - get notifications of keyspace events via pub/sub
    • secondary indexes with Redis
    • almost does not need administration
    • persistence - optional
    • clustering - distribute data among nodes

Quick References

  • Value
    • SET - set value, and reset TTL to never expire
    • GET
    • INCR - atomic increase
    • DEL
  • TTL
    • EXPIRE [key] seconds - set TTL
    • TTL [key] - get TTL, -2 if key does not exist, -1 if never expire
  • List (L)
    • RPUSH [key] [value] - push at end
    • LPUSH - push at start
    • LRANGE [key] [index first elem start from 0] [index last elem or -1 to end]
      • index out of range does not return error
    • LLEN - length
    • LPOP / RPOP
  • SET (S)
    • SADD - set add
    • SREM - set remove, 0 not found, 1 removed
    • SISMEMBER [key] [value] - set is member, 1 yes, 0 no
    • SMEMBERS - return a list of members
    • SUNION - union
  • Sorted set (Z)
    • ZADD [key] [sort_score] [value]
  • Hashes (H)
    • a map between string fields and values
    • HSET [key] [string_key] [string_value]
    • HMSET [key] [string_key] [string value] [string_key2] [string value 2]...
    • HINCRBY [key] [string_key] [increment] - increase numerical values
    • HKEYS
    • HVALS
    • HGETALL - a list of alternative key and value