Skip to content

Redis — Basics

  • In-memory key-value store. Single-threaded command execution (per shard).
  • Persistence optional (RDB/AOF). Used as cache, session store, queue, pub/sub, rate limiter, leaderboard, distributed lock.
  • Pronounced re-dis. “REmote DIctionary Server”.
TypeUse cases
Stringcached value, counter, JSON blob, bitmap
Listqueue, recent items
Hashobject/struct (fields → values)
Setunique tags, intersection/union ops
Sorted Set (ZSET)leaderboard, time-series, priority queue
Bitmap (string ops)flags per user/day, hyperlog approximations
HyperLogLogapprox unique count, tiny memory
Streamappend-only log, consumer groups (Kafka-lite)
Geospatial — GEOADD, GEORADIUS
JSON (RedisJSON module)document store
SET key val [EX 60] [NX|XX]
GET key; DEL key; EXISTS key
INCR counter; INCRBY counter 5
EXPIRE key 60; TTL key; PERSIST key
HSET user:1 name Alice email a@b
HGETALL user:1
LPUSH q job1; RPOP q
SADD tags red; SMEMBERS tags
ZADD board 100 alice; ZRANGE board 0 9 WITHSCORES REV
  • Pipelining: send N commands without waiting per reply — reduces RTT.
  • MULTI / EXEC: queue then atomic execute (no other client interleaves). No rollback — error in one command doesn’t abort.
  • Lua scripting (EVAL): atomic complex op. Runs in single-threaded context. Use for compare-and-set, multi-step ops.
  • Pub/Sub: fire-and-forget; subscribers must be connected. No persistence, no replay.
  • Streams: append log + consumer groups, persistent, ack/replay. Use for reliable delivery.
  • RDB: periodic point-in-time snapshot. Fast restart. Risk: lose last few minutes.
  • AOF (append-only file): logs every write. fsync options: always (slow, durable), everysec (default, ~1s loss), no (OS-decided).
  • Both can be enabled together. AOF rewrite compacts.
  • No persistence: pure cache role.

When maxmemory reached, policies decide what to drop:

  • noeviction — error on writes (default in newer versions).
  • allkeys-lru — least recently used (any key).
  • allkeys-lfu — least frequently used.
  • volatile-lru/lfu — only keys with TTL.
  • allkeys-random, volatile-random.
  • volatile-ttl — closest-to-expiry first.

For pure cache: allkeys-lru or allkeys-lfu typical.

  • Master-replica: async replication. Replicas read-only.
  • Sentinel: HA — monitors masters, automatic failover.
  • Cluster: sharding via 16,384 hash slots. Each key → slot via CRC16(key) % 16384. Multi-key ops only across same slot — use {tag} in key (user:{42}:profile).
  • Cluster is AP-leaning under partitions; can lose acked writes if minority isolated.
  • Single-threaded so latency-sensitive. Pipelining + connection pooling are critical.
  • Default port 6379. TLS via --tls-port and certs.
  • ACL (6.0+) — per-user perms. AUTH user pass.