Redis · Chapter 22 of 26
Memory and eviction
By the end of this chapter you can read how much memory Redis uses, give it a limit, choose what it does when the limit is reached, keep an OOM error from taking a page down, measure when a small hash stops being small, and free a big key without a pause. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Redis in production
- 4 tasks
- Redis
- In the all labs plan
The first 2 chapters of Redis are free. This one comes with the all labs plan.
Why you need this
Chapter 1 said memory is limited and dearer than disk. GullyXI pays for it by the gigabyte, and the cache Redis keeps growing: every match's page, every fan's profile copy, every computed board. Without a limit it grows until the machine runs out and the operating system kills the process, and then nothing answers at all.
Gurpreet set a limit. The next match night the cache Redis started answering errors to writes, and the match centre went down with it, though the database had the page ready. Then she copied the same settings to the points queue's Redis, and after a busy over 312 scoring jobs vanished. Venkat wants to know why fans' points were wrong. Both Redis servers did exactly what they were told; this chapter is about telling each one the right thing.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
cache.conf: a cache Redis that makes room
Write cache.conf, redis.conf lines for GullyXI's cache Redis: a memory limit of 2mb, and when it is full, evict the least recently used key from all keys (allkeys-lru). Leave out port, dir and the rest: the Check sets them.
The Check starts a Redis from your file, reads maxmemory (2097152 bytes) and maxmemory-policy (allkeys-lru), then writes 5,000 pages of 1 kB into it. No write may be refused, fewer than 5,000 pages are left, evicted_keys is above 0, and the last page written is still there.
- Task 2
measure.js: a hash against separate keys
In measure.js, measureHandles(redis, fans, bucketSize) (bucketSize 100 when left out) stores each fan's handle twice: as the string handle:<id>, and as the field <id> of the hash handles:<bucket>, where bucket is the number in the id divided by bucketSize and rounded down (u1001 goes in handles:10 with buckets of 100, and in handles:1 with buckets of 1000).
It returns { strings, hashes, encodings }: strings is the MEMORY USAGE of every handle:<id> key added up, hashes the MEMORY USAGE of every handles:<bucket> key added up, and encodings the OBJECT ENCODING of the hashes, each value once, sorted.
The Check passes GullyXI's 400 fans with buckets of 100 (five hashes, all listpack, far smaller than the strings), then 1,000 other fans, u5000 to u5999, with buckets of 1000: one hash of 1,000 fields, a hashtable.
- Task 3
full-redis.js: a full Redis must not take the page down
In full-redis.js, getMatchCentre(redis, db, matchId) reads page:match:<matchId> and returns it parsed from JSON. On a miss it calls db.getMatchCentre(matchId); a match that does not exist (null) returns null and caches nothing; otherwise it SETs the page as JSON with EX 30 and returns it.
When that SET fails because Redis is full (a ReplyError whose message starts with OOM), it still returns the page. Any other error is thrown as it is: the Check makes page:match:2002 a hash, and the WRONGTYPE error from reading it must reach the caller.
The Check calls it on a Redis with room (two opens, one database call, a TTL of 25 to 30 seconds) and on a full Redis with noeviction (the page comes back, one database call, nothing cached).
- Task 4 · interview question
Interview: queue.conf, the queue Redis loses jobs
queue.conf has the cache's settings, copied by mistake to the points queue's own Redis: maxmemory 2mb and maxmemory-policy allkeys-lru. When it filled up, Redis evicted whole queues and 312 scoring jobs vanished.
Fix it: keep maxmemory 2mb, and make a full Redis refuse new writes with an OOM error instead of removing any key (noeviction). The Check starts a Redis from your file and pushes 6,000 jobs into queue:points:0 to queue:points:4; some pushes must be refused, and every job Redis accepted must still be queued.
Farhan will ask: noeviction or allkeys-lru, for which of GullyXI's Redis servers, and why?
What you will learn
- INFO memory: how much memory Redis uses, and its limit.
- maxmemory: the limit, set with CONFIG SET (chapter 21) or in a conf file.
- The OOM error: what a full Redis answers under noeviction.
- The policies: allkeys-lru, allkeys-lfu, volatile-lru, volatile-ttl and noeviction.
- Small hashes are smaller: the listpack and where it stops.
- UNLINK for big keys, measured against DEL.
More in Redis in production
Build "Memory and eviction" yourself
GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Redis are free. This one comes with the all labs plan.