Practical System Design

Labs / Chapter 5

Every reader loads the missed product-metadata key

Read the key while it is fresh and confirm Catalog stays quiet. Then miss the entry and let every reader load alone. Share one loader, then refuse the source permit and read what the callers receive.

The system

The key product-metadata:v1 is still inside its 300 second TTL. Catalog is not called.

readslookupfetch

Client

40 readers

Client

App

Each miss loads alone

Cache

Fresh, TTL 300 s

Catalog

Idle

get_product_metadata

  1. # readers = ttl_seconds = 300
  2. def get_product_metadata(product_id):
  3. key = f"product-metadata:v1:{product_id}"
  4. cached = cache.get(key)
  5. fresh =
  6. if cached is not None:
  7. return decode(cached)
  8. product = catalog_db.fetch_metadata(product_id)
  9. cache.set(key, encode(product), ttl_seconds=300)
  10. def get_metadata(key, request):
  11. loader =
  12. permit =
  1. Cacheproduct-metadata:v1 is inside its 300 second TTL
  2. Catalog0 reads

Catalog reads for this miss

One shared loader is one read. No permit is zero reads, and the callers are unavailable.

Catalog 0.0 · Unavailable 0.0

The chart fills as the lab runs. reads is the vertical unit.

  • Catalog
  • Unavailable
  • reads