Mock CTO interview: caching a product page at 2,000 req/s and invalidating on price change

Mock CTO Interview November 13, 2026 python

You: "CDN for anonymous users, Redis with cache-aside for the data.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ======================================================================
# Q: CTO: "The product page receives 2,000 req/s. How do you cache it? What
# happens when the price changes?"
#
# You: "CDN for anonymous users, Redis with cache-aside for the data. When the
# price changes, I invalidate inside on_commit, so a price from a rolled-back
# transaction does not remain in the cache. I add jitter to the TTL so entries
# do not all expire together, and a lock or stale-while-revalidate for
# stampedes. The price at checkout is always read from the database, never from
# cache." Chapter 5.
# ======================================================================

def product_payload(pid):
    key = f'product:{pid}'
    data = cache.get(key)
    if data is None:
        data = ProductSerializer(Product.objects.get(pk=pid)).data
        cache.set(key, data, timeout=300 + random.randint(0, 60))  # TTL jitter
    return data

def change_price(product, new_price):
    with transaction.atomic():
        product.price = new_price
        product.save(update_fields=['price'])
        transaction.on_commit(lambda: cache.delete(f'product:{product.pk}'))