Lesson 07 gave you caching. This lesson gives you the tool that runs most caches in production. Redis is an in-memory data store that answers in about a millisecond, and it is also a queue, a leaderboard, a session store, and a pub/sub bus, all in one box. One checkout page load reads a session, a cached product, and a leaderboard from Redis, and the database never hears about it. I learned Redis the way I learn most tools in this job. I watched a memory-full eviction eat the sessions at 2 AM, read the config until the config confessed, and then set the eviction policy on purpose instead of by accident. This lesson walks one page load through Redis, command by command, from a first GET to the deep end: data structures, pub/sub, persistence, clustering, eviction, and the failure modes that only show up in memory.
Every hop charges rent in milliseconds. The bar below is a worked example for one checkout page load, not a measurement from a live system, but the shape is honest: in most slow page loads I have chased, the database read on a miss is most of the bill, and the Redis reads are a rounding error. Step through the lesson or switch the scenario and watch the bar change. The hot key does not change the database. It changes how long the Redis reads wait in line. The eviction storm does not change Redis. It changes how much work falls through to the database, and the database is the thing that feels it.
I start from scratch: one key, one string, one GET and one SET with a TTL — the whole store in its simplest form. Then I add one piece at a time until I reach the full picture: the full structure map (string, hash, sorted set, channel, list), the pool in front, the database behind, both persistence mechanisms, and the memory limit with its eviction policy.
App servers and the connection pool (a few dozen warm connections per server) I start here. This is the simplest piece, and nothing else exists yet.
From scratch to master. Starts from scratch: the simplest miss trip — one GET misses, the app reads the database once and writes the product back with a TTL. Builds to the Master-level picture: the cold-cache cliff (hit rate 0 percent, the whole load handed to the database at once) and the eviction incident (sessions eaten by a shared box, users logged out at 2 AM, dashboards green throughout).
A deploy adds a job caching a large catalog under a few big keys. I start here. This is the simplest piece, and nothing else exists yet.
The budget bar above shows three fixed stories. This simulator lets you write your own. It models one page load that reads from Redis and falls through to the database on a miss, with the hit rate, the traffic, the memory, and the TTL as knobs you can turn. Move a slider and watch the average read, the database load, the evictions, and the total change in the budget bar, in the hop timings, and in the status line. The numbers are a teaching example, not live measurements. The shape is the lesson: the hit rate decides the database load, memory decides the evictions, and a cold cache hands the whole load to the database at once.
Share of reads Redis answers. A hit costs 2 ms, a miss costs 45 ms, and the average is the weighted mix of the two. The database sees only the misses. At 80 percent the average read is about 11 ms and the database sees 1,000 queries a second at the default traffic.
How fast page loads arrive. The database sees the requests per second times the miss rate. More traffic with the same hit rate is more database load, in a straight line.
How full the Redis box is. Below 80 percent memory, nothing is evicted. Above 80 percent, every extra percent of memory costs 1,200 evictions a minute.
How long a key lives, in seconds. With 200,000 cached keys, the expirations per second are 200,000 divided by the TTL in seconds. A short TTL means keys expire fast and the cache refills constantly. At 3,600 seconds that is about 56 expirations a second.
Try this: (1) Leave the defaults: 80 percent hit rate, 5,000 requests a second, 60 percent memory, TTL 3,600. The total is about 14 ms and the database sees 1,000 queries a second. (2) Drop the hit rate to 0 percent. The total is about 48 ms and the database sees 5,000 queries a second. Same Redis, same code. Only the hit rate changed. (3) Set it back, then push memory to 100 percent and watch the evictions jump to 24,000 a minute. That is what a full box does while every dashboard stays green.
Three questions. No score is saved anywhere — this is just you against the material.
Redis looks like a cache with extra commands. It is not. It is four layers stacked on each other, and each layer only makes sense after the one below it. I learned it in this order, and I teach it in this order: read and write one key fast, then use the right data structure for the job, then keep the box alive and honest, then run it as a cluster. Skip a layer and the next one reads like magic. Build them in order and nothing in this lesson is magic.
Definition: Redis at the basic level is an in-memory store of keys and values, and the simplest value is a string. GET reads a string by key. SET writes one, and SET with a TTL writes one that expires on its own. Everything lives in memory, so a read answers in about a millisecond, and everything is lost when the box restarts, unless persistence is on. A key is a name, and the name is the design: session:1042, product:1042, cart:991. The colon is a convention, not a folder, and the whole key is one flat name.
Types:
Uses: when a system needs one fast read. A session. A cached product. A counter. The first win is that the database stops answering the same question a thousand times a second, because Redis answers it from memory.
Dimensions: Scale: holds up to a few hundred thousand keys on one box without special care. The commands are fast, and the limit is memory. Failure modes: a key with no TTL never expires, and a thousand of them is a leak. A restart with no persistence comes back empty. Cost: memory, per gigabyte, and memory costs more than disk. A dataset that fits in memory is cheap. A dataset that does not is a different design. Trade-offs: you get millisecond reads, and you give up durability by default. Memory forgets. If the data must survive a restart, persistence is not optional, and persistence costs disk writes on every command.
Definition: Redis as more than a cache. The value is not always a string. A hash is a map of fields, so a product can be one key with a price field and a description field, and the app can read one field without pulling the whole product. A list is an ordered queue, so a job can be pushed on one end and popped from the other. A set is unique members with no order, so a set can answer whether a user has seen an item. A sorted set is members with scores, kept ordered, so a leaderboard is a data structure, not a query. A stream is an append-only log, so an event feed can be read from where a consumer left off.
Types:
Uses: when the data structure does the work. A leaderboard as a sorted set, not a database query. A job queue as a list, not a table with a status column. A rate limit as a sorted set of timestamps, not a counter that resets at midnight. The right structure makes the query disappear, because the structure is already the answer.
Dimensions: Scale: holds to millions of keys on one box, but one big key can hurt, because Redis is single-threaded and a big key blocks every other command while it is read or written. Failure modes: a big key, usually a list or a hash that grew without a limit, blocks the single thread for milliseconds or seconds, and every other command waits. A list used as a queue with no consumer grows forever. A sorted set with millions of members is slow to read in bulk. Cost: memory, and the structures are efficient, but a big value costs the same as a thousand small ones, and the bill is the total. Trade-offs: the structures buy expressive power and cost discipline. Each one needs a size limit and a TTL policy, because a structure that grows without a limit is a memory leak with a fancier name.
Definition: Redis as a system that has to stay honest. Pub/sub is a shout: a message published to a channel reaches whoever is listening right now, and nobody else, because there is no storage and no replay. Persistence is the memory of the memory: RDB snapshots the whole dataset every so often, AOF logs every write, and a restart replays one of them. Eviction is the policy for a full box: when memory runs out, Redis removes keys by a rule, and the rule decides what dies. The rule is the design, and the default rule is rarely the right one.
Types:
Uses: when the box has to survive a restart and a full memory. Pub/sub tells every app server to drop a cached copy when a price changes. RDB plus AOF means a restart loses almost nothing. An eviction policy that fits the data means a full box evicts cached products, not live sessions. The box stops being a cache that forgets and starts being a system that can be reasoned about.
Dimensions: Scale: holds at one box with tens of millions of keys, but the persistence writes grow with the write rate, because AOF logs every write. Failure modes: a pub/sub message with no listener is gone forever. A box with no persistence restarts empty. An eviction policy of allkeys-lru on a box that holds sessions evicts live sessions. A big key blocks the single thread, and the AOF rewrite makes it worse. Cost: disk writes for persistence, and the cost of getting the eviction policy wrong, which is paid in lost sessions and cold caches. Trade-offs: RDB buys a fast restart and costs the data since the last snapshot. AOF buys almost no loss and costs disk writes on every command. Noeviction buys safety and costs rejected writes when the box is full. A policy that evicts buys uptime and costs whatever it evicts, so the policy has to match the data.
A big key is a slow command wearing a small name. One key that holds a million members makes a single read slow enough to block every other client on that shard, because Redis handles one command at a time per shard. I find big keys by scanning for them on a schedule and by watching slow log entries for commands that took more than a few milliseconds. The fix is to split the key: a hash per user instead of one hash for all users, or a sorted set per day instead of one set for all time. I never delete a big key with a single delete command in production. I delete it in pieces or let it expire, because one big delete blocks the shard just like one big read.
Definition: Redis at a size one box cannot hold. A cluster splits the keyspace into slots, 16,384 of them, and each shard owns a range of slots. A key is hashed to a slot, and the slot decides the shard. A key lives on one shard, always, and that is the fact the whole layer turns on. Replication gives each shard a replica that can take over. A hot key is a key that gets more traffic than its shard can handle, and it cannot be split by adding shards, because the key is one slot. The fixes are to split the key, to replicate the read, or to move the heat out of Redis entirely.
Types:
Uses: when one box is not enough, either for memory or for traffic. A cluster holds more keys than one box, and more reads than one box, as long as the keys spread evenly. A hot key is the exception that proves the rule, and it gets split or cached locally. The cluster is a sharding design, and the shard key is the key name, which means the naming convention from the basic layer is now a scaling decision.
Dimensions: Scale: holds at hundreds of millions of keys across dozens of shards, but the traffic has to spread, because a cluster is only as fast as its busiest shard. Failure modes: a hot key saturates one shard while the rest are idle, and no number of shards fixes it. A multi-key operation across slots fails, because the keys live on different shards. A failover that promotes a replica with stale data loses the writes since the last replication. Cost: operational. More shards mean more boxes, more replication, and more to watch, and the cluster needs an owner who understands slots. Trade-offs: a cluster buys more memory and more reads, and costs a single key that can never be split. Replication buys availability and costs lag. Client-side caching buys relief for a hot key and costs staleness, because the local copy can be out of date.
Failover is the moment a cache can lie to you. When the primary dies and a replica is promoted, a client that still writes to the old primary loses those writes, and a client that reads from the new primary before it is fully caught up reads stale data. I use the WAIT command on writes that matter, so the write is only confirmed after a replica has it, and I make the client library refresh its view of who the primary is instead of caching it forever. Then I test the failover by killing the primary on purpose during a quiet hour. The metric I watch is not how fast the new primary answers. It is how many writes were lost during the switch, and the answer has to be zero for the writes I marked as important.
Redis runs commands on one thread, in order, and that sentence explains half the incidents I have seen with it. One command at a time means a fast command is fast for everyone and a slow command is slow for everyone. A GET on a small key takes a microsecond. A GET on a key that holds a megabyte takes milliseconds, and every other command waits behind it. A KEYS command, which scans every key in the box, can block the thread for seconds. I do not run KEYS in production. I run SCAN, which walks the keyspace in small steps and lets other commands in between. The rule is simple: no command should take longer than a millisecond, and any command that does is a bug in the data, usually a key that grew too big.
When Redis runs out of memory, it does not crash. It evicts, by the policy it was given. The default in many installs is noeviction, which rejects writes when the box is full. That is honest, and it breaks writes. The policy I see most is allkeys-lru, which evicts the least recently used key, whatever it is. That is fine for a pure cache, and it is wrong for a box that holds sessions, because a session that has not been used in an hour is still a live session, and evicting it logs a user out. I pick the policy by what lives in the box. A cache gets allkeys-lru. A box with sessions gets volatile-lru, which only evicts keys with a TTL, and the sessions get their own instance if the budget allows. The policy is not a setting. It is a decision about what dies first, and it should be written down before the box is full, not after.
A big key is a key whose value is large, usually a list or a hash that grew without a limit. It is the most common Redis performance problem I have seen, and it is invisible in the hit rate, because the hits are still hits. They are just slow hits. I find big keys with the built-in scan, which reports the biggest keys by type, and I fix them the same way every time: cap the size, trim the list, or split the key. A leaderboard that keeps a million members is trimmed to the top ten thousand. A list that is a queue gets a consumer, or it gets a max length. A hash that holds a whole catalog is split into one key per product. The check I run every month is one command, and the answer is a list of keys that need attention before they block the thread at 2 AM.
Pub/sub is a shout, and I use it for exactly one kind of message: a notification that is worthless a second later. A price changed, drop your cached copy. A deploy finished, reload your config. The message has no storage, no backlog, and no replay, and that is the feature, not the bug. A job that must not be lost is not a pub/sub message. It is a list entry, or a stream entry, because a list keeps the job until someone takes it, and a stream lets a consumer resume from where it left off. I have seen a team build a job queue on pub/sub, and the jobs vanished every time a worker restarted. The workers were not broken. The tool was wrong. The fix was a list, and the jobs stopped vanishing.
At 10x the keys, the box needs a cluster, and the cluster needs a key naming convention that spreads. A key named after one hot product is a hot shard, and the fix is to split the data behind the key, not to add shards. At 100x, Redis stops being one box and becomes a tier: a cluster for the hot data, a disk store for the cold data, and a rule that moves keys between them. The eviction policy gets an owner, the big-key scan runs on a schedule, and the hit rate has an alert, because a hit rate that falls off a cliff is a database outage waiting for traffic. The cache is not a detail at this scale. It is the thing standing between the users and the database, and it gets treated like it.
# one page load: batch the reads into one round trip MULTI GET session:1042 HGETALL product:1042 ZREVRANGE leaderboard:weekly 0 9 WITHSCORES EXEC # write the product back after a miss: one hour TTL, first writer wins SET product:1042 "..." EX 3600 NX # leaderboard: add a player with a score ZADD leaderboard:weekly 1840 player:991 # the job queue is a list: push on one end, block-pop from the other LPUSH jobs:thumbnails "thumb:8841" BRPOP jobs:thumbnails 0 # a price change is a shout, not a job PUBLISH prices:changed "product:1042" # the decision that matters most: what dies first on this box CONFIG SET maxmemory 8gb CONFIG SET maxmemory-policy allkeys-lru
These are the commands as typed at the redis-cli prompt. Each argument after the command name is one part of the job.
TTL product:1042
CONFIG GET maxmemory-policy
redis-cli -h 127.0.0.1 -p 6379
MULTI starts a batch, and EXEC runs it. The reads between them go in one round trip instead of three. GET key reads a string. HGETALL key reads every field of a hash, with no field list needed.
ZREVRANGE leaderboard:weekly 0 9 WITHSCORES reads a sorted set, highest score first. 0 9 is the range, the top ten. WITHSCORES adds the score next to each member. Without it, only the names come back, and the ranking is a guess.
SET product:1042 "..." EX 3600 NX sets the value with two conditions. EX 3600 sets expiry in seconds, one hour. NX means set only if the key does not exist, so the first writer wins. Without NX, a second write would overwrite the first. Without EX, the key never expires. PX would do the same in milliseconds.
ZADD leaderboard:weekly 1840 player:991 adds a member with a score. A second ZADD with the same member updates the score. LPUSH jobs:thumbnails "thumb:8841" pushes to the head of a list. BRPOP jobs:thumbnails 0 blocks and pops from the tail. 0 means wait forever. A number like 30 means wait 30 seconds, then return nothing. Push on one end and block-pop on the other is the queue.
PUBLISH prices:changed "product:1042" sends to every subscriber. Nobody listening means the message is gone. CONFIG SET maxmemory 8gb sets the limit at runtime. CONFIG SET maxmemory-policy allkeys-lru sets what happens when the limit is hit: evict the least recently used key, any key. Without a policy, writes fail when memory is full.
TTL product:1042 shows the seconds left on the key. -1 means no expiry, -2 means the key is gone. I run it after a SET with EX to check the expiry landed. CONFIG GET maxmemory-policy reads the policy back. redis-cli -h 127.0.0.1 -p 6379 is how I open the prompt on another host. -h is the host, -p is the port. Without them, redis-cli connects to localhost on 6379.
Six pieces, one page load, and the rules that keep the box honest. The pipeline batches the reads into one round trip, because the round trips are the tax nobody counts. The SET writes the product back with a one-hour TTL and the NX flag, so only the first of two requests that miss together writes. The leaderboard is a sorted set, and the top ten is one command. The queue is a list, because a list keeps the job until a worker takes it, and pub/sub would lose it. The invalidation is a publish, because a price change is a shout that is worthless a second later. And the config at the end is the decision that matters most: this box evicts least-recently-used keys, and sessions live on their own instance, so an eviction can never log a user out.
A cache stampede is a hundred clients asking for the same missing key at the same second. The key expires, every client misses at once, and every client hits the database at once, which is the load the cache was there to prevent. I stop it two ways. First, I add a random jitter to every expiry, so the keys do not all expire together. Second, I let one client rebuild the value while the others wait on a short lock or serve the stale value for a few more seconds. The stale value is usually fine. A price that is thirty seconds old is better than a database that is down.
import random
random.seed(7)
base_ttl = 3600
keys = [base_ttl + random.randint(0, 300) for _ in range(5)]
print("expiry times (s):", keys)
print("spread:", max(keys) - min(keys), "seconds, so the keys do not all expire at once")
Expected output: five keys with a 3,600 second TTL plus up to 300 seconds of jitter expire across a 178 second spread instead of all at once. The spread is the whole fix. Jitter costs nothing, it changes no semantics, and it turns a stampede into a trickle.
This one stayed with me because Redis never crashed, never logged an error, and still logged users out at 2 AM. The box did exactly what it was told. What it was told was wrong.
A checkout service kept sessions in Redis, on the same instance as the product cache. The instance had a memory limit, and the eviction policy was allkeys-lru, the default that evicts the least recently used key, whatever it is. A deploy added a job that cached a large catalog under a few big keys, and the memory filled up in a day. Redis started evicting. It evicted the least recently used keys first, and the least recently used keys were the sessions of users who had been idle for an hour but were still logged in. Users came back to the site, clicked checkout, and were asked to log in again. The product cache was fine. The sessions were gone. The dashboards stayed green, because Redis was answering every command, the hit rate looked healthy, and the evictions were a metric nobody had alerted on.
Three things. First, sessions and cache live on separate instances now. A cache eviction can never log a user out, because the two kinds of data never share a box. Second, the eviction metric has an alert. Evictions per minute is a number that should be zero on a session instance and watched on a cache instance, and nobody had ever looked at it before this night. Third, every SET in the codebase gets a TTL, enforced in review, and the big-key scan runs weekly. The box did not fail in this incident. The policy failed, and the policy was a line of config nobody had read. The lesson is that an eviction policy is a decision about what dies first, and it should be made on purpose, before the box is full.
Design the Redis setup for a store with 5,000 requests a second. The store needs sessions for logged-in users, a product cache at 80 percent hit rate, a weekly leaderboard, a thumbnail job queue, and a price-change notification to all app servers. One product goes on flash sale every Friday and gets hammered. The memory budget is 16 GB. Sketch the instances, the data structures, the eviction policies, the persistence, and the cluster layout, and say what you would measure.
Start with the load. 5,000 requests a second at an 80 percent hit rate is 1,000 database queries a second, which is the miss tax, and at a 50 percent hit rate it is 2,500, so the database needs headroom for the day the cache goes cold. Sessions: 200,000 logged-in users with a session of about 1 KB is about 200 MB, on its own instance with noeviction and an alert at 80 percent memory, because an evicted session is a logged-out user. Product cache: 500,000 products as hashes at about 2 KB each is about 1 GB, on a second instance with allkeys-lru, because a cache can evict freely. The leaderboard is a sorted set of the top ten thousand players, small, on the cache instance. The queue is a list on a third instance with AOF persistence, because a lost job is lost work, and the queue is the truth. The flash-sale key is the problem: one key on one shard, hammered a hundred times harder on Friday, so the read is served from a local copy on each app server with a five-second TTL, and the shard never sees the heat. Persistence: RDB plus AOF on the queue instance, RDB only on the cache, because a cache can refill from the database. Memory: sessions 200 MB, cache about 1 GB, queue small, total well inside 16 GB, and the headroom is for the day the catalog doubles.
Separate instances buy isolation and cost boxes: sessions can never be evicted by a cache, but there are three instances to watch instead of one. Allkeys-lru buys uptime and costs whatever it evicts, so it lives only on the cache instance. AOF buys almost no loss and costs disk writes on every command, so it lives only on the queue. Local copies of the hot key buy relief for the shard and cost staleness, because the copy can be five seconds out of date, and a price change in those five seconds is a wrong price. A cluster buys more memory and more reads, and costs a key naming convention that has to spread, because one hot key is still one shard. And the hit rate is the metric that decides the database load, so it gets an alert, and the alert fires before the database feels it.
Saved in this browser, so the Lessons index can show your progress across the course.