Redis › Caching Patterns

What is the write through caching pattern, and how does it differ from cache aside?

Beginner

Answer

In the write through pattern, every time data is written to the database, it is also immediately written to Redis at the same time, keeping the cache always up to date, unlike cache aside where Redis is only populated when data is actually read and requested.
def update_user(user_id, data):
    database.update_user(user_id, data)
    redis.set(f'user:{user_id}', data)
Real-world example An inventory system updates both the database and Redis together whenever stock levels change, ensuring the cached inventory count is always accurate and never stale.

Common follow-ups: What is the tradeoff of writing to both the database and cache on every update?;Does write through caching increase the time a write operation takes?