Final challenge: put a cache in front of a database
After this: Count hits and time, complete the cache check, work out database load, and choose how entries leave and stay correct.
This challenge has five steps, and each step is scored on its own. You will count the hits, misses and time of six reads, complete the line of cache-aside that looks in the cache, work out the load a hit ratio leaves on the database, say which keys a full cache evicts, and say how long a time to live lets a stale copy last. The step with code shows Python and Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Count the reads
Count the reads
An empty cache with no size limit and no time to live. The app reads x, y, x, x, y, z. A hit takes 1 ms and a miss 11 ms. How many hits and misses, and how long in all?
Complete the check
Complete the first line of the function: the check that decides between a hit and a miss. The program should print 6 4.
database = {"p1": 5, "p2": 8, "p3": 3, "p4": 9}cache = {}stats = {"hits": 0, "misses": 0}
def get_price(key): __________________ # ← is the key in the cache? stats["hits"] += 1 return cache[key] stats["misses"] += 1 value = database[key] cache[key] = value return value
for key in ["p1", "p2", "p1", "p3", "p1", "p2", "p4", "p1", "p2", "p3"]: get_price(key)print(stats["hits"], stats["misses"])import java.util.*;
public class Main { static Map<String, Integer> database = Map.of("p1", 5, "p2", 8, "p3", 3, "p4", 9); static Map<String, Integer> cache = new HashMap<>(); static Map<String, Integer> stats = new HashMap<>(Map.of("hits", 0, "misses", 0));
static int getPrice(String key) { __________________ // ← is the key in the cache? stats.put("hits", stats.get("hits") + 1); return cache.get(key); } stats.put("misses", stats.get("misses") + 1); int value = database.get(key); cache.put(key, value); return value; }
public static void main(String[] args) { for (String key : List.of("p1", "p2", "p1", "p3", "p1", "p2", "p4", "p1", "p2", "p3")) { getPrice(key); } System.out.println(stats.get("hits") + " " + stats.get("misses")); }}Database load
A site makes 1,000 reads per second through a cache with a hit ratio of 0.8. How many reads per second reach the database, and what is the average time of a read?
Evict the right key
A cache with room for 3 keys evicts the least recently used. It starts empty, and the app reads a, b, c, a, d, b. What does it hold at the end, least recently used first?
How long is it wrong?
p1 = 10 is cached at 0 s with a time to live of 60 s. At 10 s the price changes to 12, but the app forgets to delete the cached copy. Reads of p1 come at 59 s and at 60 s. What do they return?
- Count the hits, misses and time of a list of reads, and complete the line of cache-aside that looks in the cache.
- Work out the reads per second left for the database, and the average time of a read, from a hit ratio.
- Evict by least recently used in a full cache, and say how long a time to live lets a stale copy last.