#278 How it works, by example 6.4 min
Why Redis Is Fast: Build a Cache with Expiry and Eviction
A cache of 100 keys out of about 1,000 answered 58% of made-up requests.
Make the session last longer
Copy the code folder first. In run.py change the time to live of session:1 from 30 to 60 and run it. Predict what the third and fourth lines will say before you look. You should see the session still returned after 31 seconds, and price:tea now missing, because the cache holds 2 keys and tea became the least recently used. The printed text (expired after 30 s) is now stale, because the session did not expire, so ignore it. Change the time to live back to 30 afterwards, because the next exercise starts from the original file.
A hint
Read the session after the clock moves, then count which key is at the front when jam is added.
Give the small cache more room
In run.py change max_keys=2 to max_keys=3 and run it. Predict the counts of hits, misses, expired and evicted. You should see evicted 0 and all three keys left in the cache.
A hint
Eviction only happens while there are more keys than max_keys.
Flatten the skew
In workload.py change skew=1.0 to skew=0.5, so popular keys are less popular, then run python run.py. Predict whether the hit rate for 100 keys will go up or down. You should see it fall far below the 58 percent from the video, which fits the video's remark that the made-up skew sets the hit rates.
A hint
A lower skew spreads requests over more keys.
Try tiny and huge cache sizes
Change the sizes in run.py from (10, 50, 100, 200, 500) to (1, 5, 20, 1000). Predict the LRU hit rate for size 1000 before you run it, then compare. You should see 95 percent against 100 percent for the top keys. Why is the LRU rate not 100 percent when the cache can hold every key?
A hint
Think about the very first request for each key, when the cache is still empty.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
This toy is not Redis: no network, no persistence, exact LRU. The workload is made up, and its skew sets the hit rates.
Up next in How it works, by example
More in How it works, by example
Related videos
- #01 Local AI Agent in Python: No API Key (Ollama) AI agents and MCP
- #02 RAG From Scratch in Python: Chunk, Embed, Retrieve (Local) RAG and vector search
- #03 pgvector vs Chroma vs Qdrant: Same Data, Real Results RAG and vector search
- #05 Structured Outputs in Python: Valid JSON Is Not Correct JSON LLM engineering and evals
