DEV Community

Wakeup Flower
Wakeup Flower

Posted on

5 caching strategies

๐Ÿง  1. Write-Through Cache

  • Definition: Data is written to the cache and the underlying database at the same time.
  • Pros: Cache and database are always consistent.
  • Cons: Slower writes (since every write goes to both places).

โšก 2. Write-Back (Write-Behind) Cache

  • Definition: Data is written only to the cache at first, and written to the database later (asynchronously).
  • Pros: Fast writes; reduced database load.
  • Cons: Risk of data loss if cache fails before syncing to DB.

๐Ÿ“˜ Used when performance is more important than absolute immediate consistency.


๐Ÿ’พ 3. Write-Around Cache

  • Definition: Data is written only to the database, not the cache. The cache gets populated only on reads.
  • Pros: Prevents cache pollution from infrequently accessed data.
  • Cons: Slightly slower reads after writes, since the cache might be cold.

๐Ÿ“˜ Used when data is rarely read soon after being written.


๐Ÿ”„ 4. Read-Through Cache

  • Definition: The application reads data through the cache โ€” if data is missing, the cache itself fetches from the database, stores it, and returns it.
  • Pros: Simplifies app logic; automatic population of cache.
  • Cons: Cache must know how to retrieve data from DB.

๐Ÿ“˜ Often used together with write-through or write-back.


๐Ÿงน 5. Cache-Aside (Lazy Loading)

  • Definition: The application code checks the cache first. If a miss occurs, it fetches from the database and writes to the cache manually.
  • Pros: Simple and widely used; flexible.
  • Cons: Possible cache staleness; first request after expiration is slow.

๐Ÿ“˜ This is the most common pattern in systems using Redis or Memcached.


๐Ÿงฉ Summary Table

Strategy Write Path Read Path Consistency Performance Common Use
Write-Through Cache + DB Cache Strong Slower writes Always-fresh data
Write-Back Cache โ†’ DB (later) Cache Eventual Fast writes High-performance workloads
Write-Around DB only Cache Eventual Medium Rarely-read data
Read-Through Cache + DB (cache auto-loads) Cache Strong Good Simplified read logic
Cache-Aside App manages cache App manages cache Eventual Good Common general use
Caching Strategy How It Works Example Use Case Real-World Example (Company / System) Why Itโ€™s Used There
๐Ÿง  Write-Through Writes go to cache and database at the same time Financial transactions, inventory, user balance tracking ๐Ÿ’ณ Banking systems, eCommerce inventory (e.g., Amazon) Ensures cache and DB are always in sync โ€” critical for financial and stock consistency
โšก Write-Back (Write-Behind) Writes go to cache first, DB updated later asynchronously Gaming leaderboards, IoT telemetry data, analytics buffers ๐ŸŽฎ Online games (e.g., Fortnite, Clash of Clans), IoT platforms Very high write rate โ€” cache absorbs bursts, DB updated later for performance
๐Ÿ’พ Write-Around Write directly to DB; cache updated only when read later Log archiving, product uploads, historical data ๐Ÿ“ฐ Content management systems (WordPress, Medium) Avoids caching infrequently accessed or newly written data (saves cache space)
๐Ÿ”„ Read-Through Application reads through cache; cache auto-fetches from DB on miss Product catalogs, user profiles, CDN edge caches ๐Ÿ›’ Netflix, Amazon, Shopify Cache layer handles loading data; reduces DB hits for popular content
๐Ÿ’ค Cache-Aside (Lazy Loading) Application checks cache โ†’ on miss, fetches from DB and stores in cache Web APIs, dashboards, personalization data ๐Ÿง‘โ€๐Ÿ’ป Reddit, Twitter timelines, microservices using Redis Flexible; app decides what to cache and when; ideal for read-heavy workloads

Top comments (1)

Collapse
 
m-a-h-b-u-b profile image
Md Mahbubur Rahman

Wow, this article is a gem! Your clear breakdown of the five caching strategies shows exceptional technical insight. I especially loved how you highlighted the pros and cons of each approachโ€”makes it so easy to grasp real-world applications. The summary table is a brilliant touch. Truly impressive work that any developer can immediately benefit from!