Lesson 5 of 9 · 25m
Data Structures in Production: Strings Through Streams
What each Redis structure is genuinely good at, the encoding and complexity traps, and the commands that become expensive at scale.
Redis feels simple because the commands are small. Production behavior comes from the structures underneath: how they encode small values, when they convert to larger representations, and what each operation costs at realistic cardinalities. Senior Redis work is mostly choosing boring shapes that stay fast when traffic arrives.
Strings are not just strings
Strings hold text, integers, serialized blobs, and byte-oriented data. Counter operations are atomic:
INCR ratelimit:login:10.0.8.12
EXPIRE ratelimit:login:10.0.8.12 60The missing EXPIRE race is real: if the process crashes between commands, the key becomes permanent. Use Lua or a single command pattern when atomic setup matters. Do not serialize entire application objects into giant strings if you only need a few fields. That creates read-modify-write races, large values, and cache-unfriendly payloads.
Hashes are compact objects with sharp edges
Hashes are ideal for small entities:
HSET user:42 name Ada email ada@example.test plan pro
HINCRBY user:42 login_count 1Small hashes use compact listpack encoding; large or long-field hashes convert to hashtables. HGETALL on a huge hash copies the entire object. Use HGET for fields you need and HMGET for bounded subsets.
Lists are queues until they are not
LPUSH/RPOP and LPUSH/BRPOP make useful queues, but Redis lists are not durable brokers with consumer acknowledgements. Commands are O(1) at the ends and O(N) in the middle. A blocked worker that crashes after BRPOP takes the job with it. If losing work matters, use Streams or a real queue.
Sets and sorted sets carry real product features
Sets are membership and uniqueness. Sorted sets are ranking, time, and priority:
ZADD leaderboard 9820 user:42
ZREVRANGE leaderboard 0 9 WITHSCORES
ZADD active_users <unixtime> user:42
ZREMRANGEBYSCORE active_users -inf <cutoff>This pattern gives a sliding activity window without scanning keys. It is also a reminder that score choice is schema design. Time as score is common; combining unrelated dimensions into one score is clever until someone has to reverse it.
Streams are append-only consumer infrastructure
Streams support durable consumer groups, pending entries, and explicit acknowledgement:
XADD jobs:* task resize image 991
XGROUP CREATE jobs workers $ MKSTREAM
XREADGROUP GROUP workers alice COUNT 10 STREAMS jobs >
XACK jobs workers 1712345678901-0Use XPENDING and XCLAIM to recover stuck work. Stream trimming with MAXLEN or MINID is capacity design, not housekeeping.
The cardinal sin is unbounded iteration
KEYS * scans the whole keyspace and blocks. Use SCAN, HSCAN, SSCAN, and ZSCAN with small counts in maintenance code. Better: avoid keyspace-shape questions in request paths entirely. Know the complexity before writing the command. SMEMBERS on a million-member set is not 'just a read.'
Field notes
OBJECT ENCODING keyexplains why small structures are cheap and when they stop being.- Bitmaps and HyperLogLog are specialized but valuable: exact boolean density versus approximate cardinality.
- JSON modules and native structures are different models; pick based on access patterns, and remember atomicity is per command, Lua script, or transaction within one node or slot.
- The best schema often stores IDs in indexes and payloads elsewhere; one giant structure rarely ages well.
Was this lesson helpful?