STAGE 04 / LESSON 06
Redis and cache expiration
A cache keeps a copy of data so repeated reads can be faster or cheaper.
THE IDEA
Understand it.
Redis is commonly used for caching, counters and other data structures. A cache entry can expire after a time-to-live (TTL). A cached copy can become stale when the underlying data changes.
Use a clear key strategy, decide how updates invalidate or refresh entries, and handle a cache miss or outage. Do not treat a cache as the only copy of important business data unless that is an intentional, tested design.
SEE IT IN CONTEXT
Cache-aside flow
Read task 42
→ cache hit? return cached value
→ cache miss? read database
→ store a short-lived copy in cache
→ return the database valueAfter a task changes, the application needs an invalidation or refresh strategy; TTL alone can leave a period of stale data.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
GitHub
GitHub engineering, 2017Redis used as a cache
GitHub described moving persistent data out of Redis while retaining Redis for caching.
- Application
- Redis cache
- Persistent data store
What to learn: Cached data can be discarded or expire, so the durable source of truth must be designed separately.
Read the source: GitHub Redis cachingShopify
Shopify engineering, 2018Redis in a commerce stack
Shopify's 2018 architecture overview lists Redis among its infrastructure technologies.
- Shopify stack in 2018
- Redis
What to learn: Redis is a real infrastructure building block, but this overview alone does not establish a particular expiration policy for a particular page.
Read the source: Shopify technology stackTRY IT YOURSELF
Practice.
What should happen when Redis is unavailable for a nonessential cache?
Show one possible answer
Where the use case permits it, fall back to the source of truth and monitor the failure. Protect the database from a sudden surge in fallback requests.
GO DEEPER