Caching Your results Progress Loading your progress
Results and the cheat sheet fill in as you go. Nothing here is locked; come back whenever you like.
Next in this path: Databases at scale: replicas, sharding and the trade-offs · 6 lessons · 1 h 10 min
Mastery by outcome Each outcome is backed by the checks that test it. Solid means every check was right first time; with hints means you got there with a nudge; worth a revisit means an answer was revealed or is still wrong.
1 Say what a cache, a hit and a miss are, and work out how long a list of reads takes with a cache and without one.
Not yet 2 Write cache-aside code: look in the cache first, and on a miss read the database, store the answer in the cache, then return it.
Not yet 3 Work out what a hit ratio buys: the average time of a read, and how many reads still reach the database each second.
Not yet 4 Choose how cached answers go away and stay correct: a time to live, a size limit with least recently used eviction, and deleting the cached copy on every write.
Not yet Your cheat sheet What each lesson leaves you able to do, in one place. Lessons you have not finished are dimmed, not hidden.
Say what a cache is: a small, fast store of answers already fetched, looked at before the slow database. Tell a hit from a miss: a hit is answered by the cache in 1 ms; a miss checks the cache, reads the database and stores the answer, 11 ms in all. Count the ten reads p1 p2 p1 p3 p1 p2 p4 p1 p2 p3: 6 hits and 4 misses, 50 ms in all against 100 ms with no cache. Say that only misses reach the database: 4 database reads instead of 10. List the four steps of cache-aside: look in the cache; on a hit return; on a miss read the database; store the answer, then return it. Say where the cache sits: beside the app server, which talks to the cache and the database separately. Write the price function with a map as the cache, and run it over the ten reads to print 6 4. Say that a cache inside each app server is fast but not shared, while a cache server such as Redis is shared but reached over the network. Work out a hit ratio: hits divided by all reads, 0.6 for the ten reads. Work out the average time of a read: h × 1 + (1 − h) × 11 = 11 − 10h ms, so 0.9 gives 2 ms. Work out the reads left for the database: 1,000 × (1 − h) per second, 100 at 0.9 and 10 at 0.99. Say why a cache with a hit ratio of 0 makes reads slower: 11 ms against 10 ms with no cache. Say when a copy expires: stored at t with a time to live, it is used while the time is before t + TTL, and reads do not reset the clock. Work the six reads of p1 under a 30 s time to live: M H H M H M, 3 hits and 3 misses. Say what eviction is: removing a key from a full cache to make room for a new one. Evict by least recently used: with room for 2, reads a b a c b give M M H M M and leave c, b. Say what a stale answer is: a hit on a cached copy that no longer matches the database. Delete the cached copy on every write (invalidation), so that the next read fetches the new value: 12, 12, 12 instead of 10, 10, 12. Say why deleting is safer than updating the cached copy: a delete has no value to get wrong when two writes race. Keep a time to live as a safety net: it limits how long a copy that a missed delete or a slow read left behind can last. Name write-through, write-back and CDNs as other places the same ideas appear. 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.