Design Doc · Catalog Team · In Review
Add a Read-Through Cache to the Catalog API
Press Enter to flag this paragraph.
Context
The catalog API serves product pages and checkout. It returns each product's details and its price in the shopper's currency. p99 read latency is 420 ms, and 80% of reads go to the top 2,000 products.
Goals
Bring p99 latency for product reads under 80 ms, and cut read load on the primary Postgres database by at least half.
Design
We add a Redis cluster in front of Postgres. API responses are cached under the key product:{id}.
On a cache miss, the API reads the product from Postgres, writes the result to Redis, and responds.
When a product changes, the writer deletes the cache key and then updates Postgres. Keys expire after 24 hours.
The cache is flushed on every deploy, so new code never reads an entry written in an old format.
Failure Handling
If Redis is unavailable, the API returns 503 so it never serves inconsistent data.
Capacity
Redis gets 16 GB of memory, twice the size of the full catalog.
Monitoring
We'll alert on Redis CPU and memory.
Rollout
Ship behind a flag at 5% of traffic, then raise it daily while watching p99 latency and Postgres read load.