Skip to main content
PreparationZone Logo
PrepZone

Caching Patterns

Cache-aside, read-through, write-through, and write-behind — with VaultCommerce cart examples.

Pass checkpoint below

Read these first

Why this matters

  • VaultCommerce's cart, catalog, and session layers use different patterns because they have different freshness and durability needs.
  • Picking the wrong pattern causes double writes, lost updates, or caches that never invalidate.
  • Interviews ask you to diagram cache-aside and defend when write-through is worth the latency tax.

Cache-aside (lazy loading)

The application owns cache logic — check cache, on miss load DB, populate cache.

App
RedisCache
PostgresSource of truth
Redis
App checks cache first. On miss, reads DB and populates cache.
Java
public Product getProduct(String sku) {
    String key = "product:" + sku;
    Product cached = redis.get(key);
    if (cached != null) return cached;

    Product product = productRepo.findBySku(sku);
    if (product != null) {
        redis.setex(key, Duration.ofMinutes(10), product);
    }
    return product;
}

VaultCommerce uses cache-aside for product pages — simple, debuggable, and the database remains source of truth. Invalidation happens on catalog update events.

Read-through and write-through

AspectRead-throughWrite-through
Who loads on missCache libraryApplication
Write pathApp writes DB onlyApp writes cache; cache writes DB
ConsistencyCache always populated on readCache and DB updated together
VaultCommerceNot used — prefer explicit controlSession tokens (short TTL)
  • Who loads on miss

    Read-throughCache library
    Write-throughApplication
  • Write path

    Read-throughApp writes DB only
    Write-throughApp writes cache; cache writes DB
  • Consistency

    Read-throughCache always populated on read
    Write-throughCache and DB updated together
  • VaultCommerce

    Read-throughNot used — prefer explicit control
    Write-throughSession tokens (short TTL)

Write-through adds write latency because every update hits cache and database synchronously. VaultCommerce uses it only for session tokens where stale reads are unacceptable and objects are small.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Write-behind (write-back)
  • VaultCommerce pattern map
  • Invalidation strategies

Test yourself

Answer these before moving on — recall is what makes it stick.