0
0
Redisquery~10 mins

Write-behind pattern in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Write-behind pattern
Client writes data to cache
Cache stores data immediately
Write operation queued
Background process writes data to DB
DB updated asynchronously
Client reads get latest from cache
Client writes data to cache first; cache stores it and queues write to database. Background process updates DB later, so writes are asynchronous.
Execution Sample
Redis
SET user:1:name "Alice"
# Cache stores immediately
# Write queued
# Background writes to DB
GET user:1:name
Client sets a value in Redis cache; cache stores it immediately and queues DB write; client reads from cache.
Execution Table
StepActionCache StateWrite QueueDB StateOutput
1Client issues SET user:1:name "Alice"{"user:1:name": "Alice"}[('user:1:name', 'Alice')]{"user:1:name": null}OK
2Write operation queued for DB{"user:1:name": "Alice"}[('user:1:name', 'Alice')]{"user:1:name": null}
3Background process writes to DB{"user:1:name": "Alice"}[]{"user:1:name": "Alice"}
4Client issues GET user:1:name{"user:1:name": "Alice"}[]{"user:1:name": "Alice"}"Alice"
5No more writes queued, process idle{"user:1:name": "Alice"}[]{"user:1:name": "Alice"}
💡 Write queue empty, DB updated, client reads latest from cache
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Cache{}{"user:1:name": "Alice"}{"user:1:name": "Alice"}{"user:1:name": "Alice"}{"user:1:name": "Alice"}{"user:1:name": "Alice"}
Write Queue[][('user:1:name', 'Alice')][('user:1:name', 'Alice')][][][]
DB{}{}{}{"user:1:name": "Alice"}{"user:1:name": "Alice"}{"user:1:name": "Alice"}
OutputOK"Alice"
Key Moments - 3 Insights
Why does the client get the data immediately after SET even though the DB is not updated yet?
Because the cache stores the data immediately (see Step 1 in execution_table), so GET reads from cache, not DB.
What happens if the background process fails to write to the DB?
The write queue will keep the operation until successful, preventing data loss (Step 2 and 3 show the queue and write).
Why is the write to DB asynchronous and not immediate?
To improve performance and reduce client wait time, writes are queued and done in background (Step 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the write queue after Step 3?
AContains multiple write operations
BContains one write operation
CEmpty list []
DUnknown
💡 Hint
Check the 'Write Queue' column at Step 3 in execution_table
At which step does the DB get updated with the new data?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'DB State' column in execution_table to see when data changes from null to 'Alice'
If the client reads data immediately after Step 1, what value will it get?
A"Alice"
Bnull
CError
DOld DB value
💡 Hint
Check the 'Cache State' and 'Output' columns at Step 1 and Step 4
Concept Snapshot
Write-behind pattern:
- Client writes to cache immediately
- Write operation queued for DB asynchronously
- Background process updates DB later
- Reads get latest from cache
- Improves write performance by decoupling DB writes
Full Transcript
The write-behind pattern means when a client writes data, it first goes to the cache. The cache stores it right away and queues the write to the database. A background process then writes the data to the database asynchronously. This way, the client gets fast responses from the cache, and the database is updated later without blocking the client. The execution table shows the cache state, write queue, and database state step-by-step. The client reads the latest data from the cache even before the database is updated. This pattern improves performance by separating cache writes from slower database writes.