Practical System Design. Cache Roulette

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. Miss80 readers on one key
  2. Catalog0 reads

product-metadata:v1

Cache-aside, TTL 300 seconds. A fresh entry does not call Catalog. A miss is one key.

Catalog reads 0

each loadsCatalog 0
0 readers each call catalog_db.fetch_metadata for this missed key.

Live metrics

ReadyThe key is product-metadata:v1. A fresh entry returns from cache without calling Catalog.
Metric definitions
MetricValueWhat it means
Catalog reads0fetch_metadata calls this miss produces.
Unavailable0Readers who got source_read_limit.
TTL300sHow long a fresh product-metadata entry is kept.
Readers0Requests on this one key.
0
0
300 s
80