Redis — Basics
Redis — Basics
Section titled “Redis — Basics”What it is
Section titled “What it is”- 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”.
Data types
Section titled “Data types”| Type | Use cases |
|---|---|
| String | cached value, counter, JSON blob, bitmap |
| List | queue, recent items |
| Hash | object/struct (fields → values) |
| Set | unique tags, intersection/union ops |
| Sorted Set (ZSET) | leaderboard, time-series, priority queue |
| Bitmap (string ops) | flags per user/day, hyperlog approximations |
| HyperLogLog | approx unique count, tiny memory |
| Stream | append-only log, consumer groups (Kafka-lite) |
| Geo | spatial — GEOADD, GEORADIUS |
| JSON (RedisJSON module) | document store |
Common commands
Section titled “Common commands”SET key val [EX 60] [NX|XX]GET key; DEL key; EXISTS keyINCR counter; INCRBY counter 5EXPIRE key 60; TTL key; PERSIST key
HSET user:1 name Alice email a@bHGETALL user:1LPUSH q job1; RPOP qSADD tags red; SMEMBERS tagsZADD board 100 alice; ZRANGE board 0 9 WITHSCORES REVPipelining vs transactions
Section titled “Pipelining vs transactions”- 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 vs Streams
Section titled “Pub/Sub vs Streams”- 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.
Persistence
Section titled “Persistence”- 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.
Eviction
Section titled “Eviction”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.
Replication & cluster
Section titled “Replication & cluster”- 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.
Connection
Section titled “Connection”- Single-threaded so latency-sensitive. Pipelining + connection pooling are critical.
- Default port 6379. TLS via
--tls-portand certs. - ACL (6.0+) — per-user perms.
AUTH user pass.