0
0
Redisquery~10 mins

Cache stampede prevention in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cache stampede prevention
Client requests data
Check cache for data
Return cached
Lock acquired?
Fetch from DB
Update cache
Return data to client
When a client requests data, the system checks the cache. If missing, it tries to acquire a lock to fetch from the database and update the cache. Others wait for the cache to be updated to prevent many DB hits.
Execution Sample
Redis
if cache.exists(key):
    return cache.get(key)
if cache.setnx(lock_key, 1):
    data = db.query()
    cache.set(key, data)
    cache.delete(lock_key)
else:
    wait_for_cache_update()
    return cache.get(key)
This code checks cache, acquires a lock to fetch DB data if missing, updates cache, and others wait to avoid multiple DB hits.
Execution Table
StepActionCache StateLock StateDB QueryReturned Data
1Client requests dataemptyno lockno querynone
2Check cache for keyemptyno lockno querynone
3Cache miss, try to set lockemptylock setno querynone
4Lock acquired, query DBemptylock setquery executednone
5Update cache with datadata cachedlock setquery donenone
6Delete lockdata cachedno lockquery donenone
7Return cached datadata cachedno lockquery donedata returned
8Another client requests datadata cachedno lockno querydata returned
9Check cache for keydata cachedno lockno querydata returned
10Cache hit, return datadata cachedno lockno querydata returned
11Client requests data, cache emptyemptylock set by anotherno querynone
12Lock not acquired, waitemptylock setno querynone
13Cache updated by lock ownerdata cachedno lockquery donenone
14Return cached data after waitdata cachedno lockquery donedata returned
💡 Execution stops after data is returned from cache or DB and lock is released.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6After Step 10After Step 14
cacheemptyemptydata cacheddata cacheddata cacheddata cached
lockno locklock setlock setno lockno lockno lock
db_queryno queryno queryquery executedquery doneno queryquery done
returned_datanonenonenonenonedata returneddata returned
Key Moments - 3 Insights
Why do we use a lock before querying the database?
The lock prevents multiple clients from querying the database simultaneously when the cache is empty, as shown in steps 3-6 in the execution_table.
What happens if a client cannot acquire the lock?
The client waits for the cache to be updated by the lock owner, then reads from the cache, as shown in steps 11-14.
Why do we delete the lock after updating the cache?
Deleting the lock signals other clients that the cache is ready to be read, preventing deadlocks and allowing them to get data from cache (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is happening?
ACache is being updated with data
BClient returns cached data
CDatabase query is executed after lock acquired
DLock is deleted
💡 Hint
Check the 'DB Query' and 'Lock State' columns at step 4 in execution_table.
At which step does the lock get released?
AStep 3
BStep 6
CStep 5
DStep 10
💡 Hint
Look at the 'Lock State' column in execution_table to find when it changes from 'lock set' to 'no lock'.
If the cache is already filled, what happens when a client requests data?
AClient returns cached data immediately
BClient queries the database
CClient waits for lock to be released
DClient sets a new lock
💡 Hint
Refer to steps 8-10 in execution_table where cache is 'data cached'.
Concept Snapshot
Cache Stampede Prevention:
- Check cache first for data.
- If missing, try to set a lock (setnx).
- Lock owner queries DB and updates cache.
- Others wait for cache update to avoid multiple DB hits.
- Delete lock after cache update to release waiting clients.
Full Transcript
Cache stampede prevention is a technique to avoid many clients querying the database at once when cache data is missing. The process starts when a client requests data and checks the cache. If the data is not found, the client tries to acquire a lock. If successful, it queries the database, updates the cache, and releases the lock. Other clients that fail to get the lock wait until the cache is updated and then read from the cache. This prevents multiple expensive database queries and reduces load. The execution table shows each step, including cache state, lock state, database queries, and returned data. Key moments include why the lock is used, what happens when the lock is not acquired, and why the lock is deleted after updating the cache. The visual quiz tests understanding of these steps. The snapshot summarizes the main points for quick reference.