Module 02 · From Scratch · Redis · with code: Go · about 20 min to read
Continue where you left off ×
Eviction: Memory Is Full
Eviction Memory Is Full · Open board
The board behind Build Your Own Redis, From Scratch , where eviction got one paragraph. This is that paragraph, unpacked.
Before you start. You know that Redis keeps everything in RAM, and that a key can carry its own expiry date so it deletes itself later: that was Keys That Die (Expiry) . This lesson is about the key that carries no expiry date, when the box it lives in runs out of room anyway.
The idea in 30 seconds
Eviction is Redis choosing to delete a key that never asked to leave, because a write needs room and nothing else is free.
The perfect eviction rule needs to know the future. No real system has that, so every real system guesses.
Redis refuses to track its guess exactly, because the bookkeeping itself would cost the memory it is trying to save.
Instead it keeps one cheap stamp per key and samples a handful at eviction time, LRU guesses by recency, LFU guesses by frequency.
LFU exists because recency lies to you the moment something scans the whole database once.
Whether to guess at all, and how loudly to fail when the guess runs out, depends on whether the data can be rebuilt or Redis is the only copy.
Read the story
Lesson 9 of 117 in From Scratch
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1966: the rule that needs a crystal ball A write arrives with nowhere to go The list Redis refuses to keep A cheap watch instead of a diary When “recently” lies to you A wall you want, and a wall you don’t Terms it explains: evicted, LRU, sample, LFU, [the slate lab](/labs/the-slate/), logarithmic, Decay.
Review From Scratch
Mark as read Saved in this browser, and to your account when you sign in.