Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - Write-through pattern
Client writes data
↓
Write to Cache
↓
Write to Database
↓
Confirm write success
↓
Return success to Client
The write-through pattern writes data to the cache and database at the same time, ensuring cache and database are always synchronized.
Execution Sample
Redis
SET user:1 "Alice"
// Write to cache
SETDB user:1 "Alice"
// Write to database
RETURN success
This example shows writing a user name to both Redis cache and the database before confirming success.
Execution Table
StepActionCache StateDatabase StateResult
1Client sends write request for key 'user:1' with value 'Alice'{}{}Request received
2Write 'Alice' to cache key 'user:1'{"user:1": "Alice"}{}Cache updated
3Write 'Alice' to database key 'user:1'{"user:1": "Alice"}{"user:1": "Alice"}Database updated
4Confirm write success to client{"user:1": "Alice"}{"user:1": "Alice"}Success returned
5End of write-through operation{"user:1": "Alice"}{"user:1": "Alice"}Operation complete
💡 Write-through completes after both cache and database are updated successfully.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Cache{}{"user:1": "Alice"}{"user:1": "Alice"}{"user:1": "Alice"}
Database{}{}{"user:1": "Alice"}{"user:1": "Alice"}
Key Moments - 2 Insights
Why do we write to the cache before the database?
Writing to the cache first ensures the cache always has the latest data when the database write succeeds, as shown in steps 2 and 3 of the execution_table.
What happens if the database write fails after cache is updated?
In the write-through pattern, failure handling is needed to keep cache and database consistent. The example assumes success, but in real use, rollback or retry logic is required after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache state after step 2?
A{"user:1": "Bob"}
B{"user:1": "Alice"}
C{}
Dnull
💡 Hint
Check the 'Cache State' column in row for step 2.
At which step does the database get updated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing database write.
If the database write fails, what should happen to keep cache consistent?
ARollback cache update or retry database write
BDo nothing, cache stays updated
CClear the database
DReturn success anyway
💡 Hint
Refer to key_moments explanation about failure after step 3.
Concept Snapshot
Write-through pattern:
- Write data to cache and database simultaneously
- Cache always has latest data
- Ensures strong consistency
- Write fails if either cache or database fails
- Used when cache must reflect database immediately
Full Transcript
The write-through pattern means when a client writes data, the system writes it first to the cache, then to the database. This keeps both cache and database synchronized. The example shows writing a key 'user:1' with value 'Alice'. Step 1 receives the request. Step 2 writes to cache. Step 3 writes to database. Step 4 confirms success to client. Variables track cache and database states changing from empty to holding the new data. Key moments include why cache is written first and what happens if database write fails. The visual quiz checks understanding of cache state after step 2, when database updates, and failure handling. The snapshot summarizes the pattern as writing to cache and database together to keep data consistent.

Practice

(1/5)
1. What is the main purpose of the write-through pattern in Redis caching?
easy
A. To delete data from the cache when the database is updated
B. To update only the cache and delay database writes
C. To read data only from the cache without updating the database
D. To update both the cache and the database at the same time

Solution

  1. Step 1: Understand write-through pattern basics

    The write-through pattern means writing data to the cache and database simultaneously to keep them in sync.
  2. Step 2: Identify the correct behavior in Redis context

    In Redis, this pattern ensures cache is updated immediately when the database changes, avoiding stale data.
  3. Final Answer:

    To update both the cache and the database at the same time -> Option D
  4. Quick Check:

    Write-through = update cache + database [OK]
Hint: Write-through updates cache and DB together [OK]
Common Mistakes:
  • Thinking write-through updates only cache
  • Confusing write-through with write-back pattern
  • Assuming cache is updated after database asynchronously
2. Which Redis command sequence correctly implements a write-through cache update when saving a user profile with key user:123 and value {"name":"Alice"}?
easy
A. DEL user:123 then SET user:123 '{"name":"Alice"}'
B. GET user:123 then SET user:123 '{"name":"Alice"}'
C. SET user:123 '{"name":"Alice"}' followed by DB update
D. SET user:123 '{"name":"Alice"}' only, no DB update

Solution

  1. Step 1: Identify write-through steps

    Write-through requires writing to cache (Redis) and database together.
  2. Step 2: Match Redis commands to write-through

    Using SET updates Redis cache; followed by DB update ensures both are in sync.
  3. Final Answer:

    SET user:123 '{"name":"Alice"}' followed by DB update -> Option C
  4. Quick Check:

    SET + DB update = write-through [OK]
Hint: SET cache then update DB for write-through [OK]
Common Mistakes:
  • Only updating cache without DB
  • Deleting cache before setting without DB update
  • Getting cache before setting without DB write
3. Consider this pseudo-code using write-through pattern:
cache.SET('item:1', 'value1')
database.UPDATE('item:1', 'value1')
result = cache.GET('item:1')
What will result contain after execution?
medium
A. null
B. 'value1'
C. Old cached value
D. Error due to missing database read

Solution

  1. Step 1: Analyze cache write

    The cache is set to 'value1' for key 'item:1' before database update.
  2. Step 2: Analyze cache read

    Reading from cache immediately after setting returns the new value 'value1'.
  3. Final Answer:

    'value1' -> Option B
  4. Quick Check:

    Cache GET after SET returns new value [OK]
Hint: GET after SET returns updated cache value [OK]
Common Mistakes:
  • Assuming cache GET returns null if DB updated
  • Thinking old cached value remains after SET
  • Expecting error without explicit DB read
4. You implemented write-through caching but notice stale data in Redis after database updates. Which mistake likely causes this?
medium
A. Updating database but forgetting to update Redis cache
B. Updating Redis cache but not the database
C. Using GET instead of SET to update cache
D. Deleting database entries without clearing cache

Solution

  1. Step 1: Identify cause of stale cache

    If database updates but cache is not updated, cache holds old data causing staleness.
  2. Step 2: Match mistake to write-through pattern

    Write-through requires cache update on every DB write; missing cache update breaks this.
  3. Final Answer:

    Updating database but forgetting to update Redis cache -> Option A
  4. Quick Check:

    Missing cache update causes stale data [OK]
Hint: Always update cache when DB changes [OK]
Common Mistakes:
  • Updating cache only without DB
  • Using GET to update cache instead of SET
  • Not clearing cache on DB deletes
5. You want to implement a write-through cache in Redis for a product catalog. Which approach best ensures data consistency and minimal stale reads?
hard
A. On product update, update both Redis cache and database synchronously in the same operation
B. On product update, update the database first, then delete the Redis cache entry
C. On product update, update only the database and rely on cache expiration to refresh
D. On product update, write to Redis cache first, then update the database asynchronously

Solution

  1. Step 1: Understand write-through consistency needs

    Write-through requires cache and database to be updated together to avoid stale reads.
  2. Step 2: Evaluate options for consistency

    Updating both synchronously ensures cache always reflects latest DB state, minimizing stale data.
  3. Final Answer:

    On product update, update both Redis cache and database synchronously in the same operation -> Option A
  4. Quick Check:

    Synchronous cache + DB update = consistent write-through [OK]
Hint: Update cache and DB together synchronously [OK]
Common Mistakes:
  • Updating cache before DB asynchronously causing race conditions
  • Deleting cache instead of updating it
  • Relying only on cache expiration for freshness
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard