Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Practice: Caching Exercises

Practice distributed caching with hands-on exercises: capacity math, weighted rings, debugging a stale cache, key design, plus review questions with answers.

Reading about caching is not the same as reasoning about it under pressure. This practice lesson gives you five exercises that mirror real work (sizing, hashing, debugging, key design and incident analysis) and a set of review questions with short answers. Every code editor runs a starter that prints a progress message; edit it until the checks say PASS. Solutions follow each exercise as read-only code, so cover them until you have tried.

Exercise 1: how many database queries?

A page view needs 12 cached objects. The site serves 2,000 page views per second. The cache hit ratio is 96%.

  1. How many cache lookups per second?
  2. How many reach the database?
  3. If an outage of one of 5 equal nodes sends that node's share of keys to the database (hit ratio drops by the lost share), how many database queries per second now?
Python Exercise 1: sizing the database load

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution:

Python
lookups = views_per_second * objects_per_view                  # 24,000
db_queries = round(lookups * (1 - hit_ratio))                  # 960
survivors_ratio = hit_ratio * (nodes - 1) / nodes              # lost node = misses
db_after_failure = round(lookups * (1 - survivors_ratio))      # 5,568

The database load grows almost sixfold because one node out of five held 20% of the hits. See Node Failure and Failover.

Exercise 2: a weighted hash ring

You have two nodes: small (4 GB) and large (12 GB). Write build_ring(weights, points_per_unit) so that each node gets weight * points_per_unit points, and check that the large node ends up with roughly three times the keys.

Python Exercise 2: weights on the ring

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution:

Python
def build_ring(weights, points_per_unit):
    points, owner = [], {}
    for node, weight in weights.items():
        for i in range(weight * points_per_unit):
            p = h(f"{node}#{i}")
            points.append(p)
            owner[p] = node
    points.sort()
    return points, owner

Background: Virtual Nodes and Load Balance.

Exercise 3: find the bugs

The helper below caches a user profile. It has four problems from this track. Find them before you read the list.

Python
def get_profile(user_id):
    key = "profile"
    value = cache.get(key)
    if value:
        return value
    value = db.load_profile(user_id)
    cache.set(key, value)
    return value

def update_profile(user_id, data):
    cache.delete("profile")
    db.save_profile(user_id, data)

Problems:

  1. The key has no user id, so everyone shares one profile (Cache Anti-Patterns and Security).
  2. No TTL: a wrong value would stay until eviction.
  3. if value: treats falsy values as misses, such as an empty profile or 0; compare with None. A missing user also triggers a database query each time (no negative caching).
  4. Delete before update opens a race where a reader refills the cache with old data (Stale Data and Race Conditions).

Now write the corrected version and test it against a stand-in cache:

Python Exercise 3: fix the cache helper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution sketch:

Python
MISSING = "__missing__"

def get_profile(user_id):
    key = f"profile:v1:{user_id}"
    value = cache.get(key)
    if value is not None:
        return None if value == MISSING else value
    value = db.load_profile(user_id)
    cache.set(key, MISSING if value is None else value, ttl=300)
    return value

def update_profile(user_id, data):
    db.save_profile(user_id, data)
    cache.delete(f"profile:v1:{user_id}")

The database read count works out to 4: users 1, 2 and 99 once each, plus user 1 again after the update.

Exercise 4: stack the TTLs

Your promise: users never see data older than 90 seconds. The layers are a browser (max-age), a CDN and Memcached. The browser and CDN TTLs must be at least 10 seconds each to be useful. Memcached should be as long as possible within the budget. Find a valid plan.

Python Exercise 4: a TTL budget

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution: memcached = budget - browser - cdn = 70. In practice you might shorten the browser TTL to zero for dynamic pages and give the CDN more. See Multi-Layer Caching and CDNs.

Exercise 5: read the stats

An operator pastes these numbers from two samples taken 60 seconds apart:

Counter Sample 1 Sample 2
get_hits 4,000,000 4,450,000
get_misses 500,000 650,000
evictions 120,000 150,000
Python Exercise 5: rates from two samples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution:

Python
hits = s2["hits"] - s1["hits"]                     # 450,000
misses = s2["misses"] - s1["misses"]               # 150,000
interval_hit_ratio = hits / (hits + misses)        # 0.75
gets_per_second = (hits + misses) / seconds        # 10,000
evictions_per_second = (s2["evictions"] - s1["evictions"]) / seconds   # 500

The cumulative hit ratio is about 87%, but the current ratio is 75%: the cumulative figure hides a recent decline. Always compute rates from differences (Hit Ratio and Metrics). The 500 evictions per second hint that memory is short.

Review questions

  1. Why can't Memcached servers rebalance data themselves? They are independent and do not know about each other; the client chooses the server.
  2. What does consistent hashing guarantee when a node is added? Only the keys in the new node's arcs move, and they all move to the new node.
  3. Name three ways to protect against a stampede. An add lock (single flight), serving stale while refreshing, probabilistic early refresh (and TTL jitter, warm-up).
  4. When is cas the right tool? For concurrent read-modify-write on one value, with retries on EXISTS.
  5. A key is hot. What can you do? Replicate it under several names, add a short in-process cache, or split the object.
  6. Why does the order "update the database, then delete the cache" matter? It narrows the window in which a reader can refill the cache with the old value.
  7. How do you invalidate thousands of related keys at once? Include a version in the key and increment it.
  8. Redis or Memcached? Memcached for simple, high-volume caching of values; Redis when you need data structures, persistence, replication or messaging; many sites use both (Memcached vs Redis).
  9. Why is an open port 11211 dangerous? There is no authentication by default; anyone could read, change or flush data and abuse the server.
  10. What does "memory is full but there are no evictions" mean? Expired items are being reclaimed; that is normal.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Hit ratio is 96% with 24,000 lookups per second. How many requests reach the database?

    4% of 24,000 is 960.
  2. 2. Why compute the hit ratio from the difference of two samples?

    The cumulative ratio averages the whole uptime; the interval ratio shows the present.
  3. 3. Which fix belongs to a cached profile keyed only as 'profile'?

    Everything that changes the output must be part of the key.
  4. 4. A browser TTL of 10 s, CDN 10 s and Memcached 70 s give what worst-case staleness?

    Layer TTLs add up in the worst case.

Summary

  • You sized database load from hit ratios and failure scenarios.
  • You built a weighted hash ring, fixed a buggy cache helper and budgeted layered TTLs.
  • You turned two counter samples into rates and saw why cumulative ratios mislead.
  • The review questions cover the whole track; revisit any that you could not answer.

Next: Back to the track overview