Live Request Simulator
Visualize the difference between direct database access and cached responses.
How: Caching Patterns
There isn't one way to cache. The pattern you choose dictates data consistency and system complexity.
Cache-Aside (Lazy Loading)
The most popular pattern. The application is responsible for reading and writing to the cache.
thumb_up Pros
- check_circleResilient to cache failure (app just goes to DB)
- check_circleData model can differ from DB
thumb_down Cons
- cancelFirst request is slow (cold start)
- cancelData can become stale
Workflow
- 1. App asks Cache for data.
- 2. If HIT, App returns data.
- 3. If MISS, App queries Database.
- 4. App writes data to Cache for next time.
When Should You Cache?
Caching introduces complexity. It's not a silver bullet. Test your intuition with these real-world scenarios.
Daily News Article
The text of a news article published this morning. It is read by millions but edited rarely.
Real-time Stock Price
A stock ticker that changes every millisecond and requires strict accuracy for traders.
Complex Search Query
A search for "red shoes size 10" involving 4 table joins that takes 2 seconds to run.
Eviction Policies: LRU
Memory is finite. What happens when the cache is full?
LRU (Least Recently Used) is the industry standard.
Cache Memory (Max 4 Items)
How LRU Works
1. When you access an item, it moves to the top (Most Recently Used).
2. If you add a new item and the cache is full (4/4), the item at the bottom (Least Recently Used) is evicted (deleted) to make room.