Redis › Caching Patterns

What is the write behind, also called write back, caching pattern, and what risk does it introduce?

Intermediate

Answer

In the write behind pattern, data is written to Redis immediately and then asynchronously written to the database a short time later, which improves write speed since the application does not wait for the slower database, but introduces the risk of losing recently written data if Redis crashes before that data reaches the database.
redis.set(f'order:{order_id}', order_data)
-- A background worker later reads from Redis
-- and writes the same data to the database
Real-world example A high throughput analytics system writes events to Redis instantly for fast response times, using a background worker to batch and persist those events to the database a few seconds later.

Common follow-ups: How do you minimize the risk of data loss with write behind caching?;What kind of applications benefit most from this pattern despite the risk?