Serving millions of redirects
After this: Put a cache in front of the database for redirects, size it, and work out the database reads it leaves.
On this page · 6 sections · 3 checks
Lesson 1 found 3,858 redirects a second and only 38.6 new links. Every redirect asks the same question, "which long URL belongs to this code?", and many people open the same popular links. The Caching tutorial was about questions like that: keep the answer close by, and the database does not have to answer it again.
Cache-aside on the code
A here holds pairs of code → long URL. A redirect uses it in the cache-aside way from the Caching tutorial:
- The app server looks for the code in the cache. If it is there, that is a : answer at once.
- If it is not there, that is a : read the database, store the pair in the cache, and answer.
With this learning path's teaching numbers, cache 1 ms and database 10 ms, a hit costs 1 ms and a miss 11 ms, because the cache was checked first. The numbers are round on purpose; real ones vary by a factor of several, and the order of size is the point. Here is a miss, one message at a time:
Press Run it to send the messages one at a time.
- messages
- 0
A new link is not put in the cache when it is made. It gets there on its first redirect. Links never change once made, so a cached pair stays right for as long as the link exists.
The whole flow, step by step
Two app servers sit behind a load balancer, which hands requests to them in turn. They share one cache and one database. The counter stands at 125, so three new links get the codes 21, 22 and 23. Then eight redirects arrive: 21 21 22 21 23 21 22 21.
- Short links: 11 requestsRun it and step through. The first three requests make links 21, 22 and 23 and write them to the database. Then watch the Redirects strip: which redirect of each code is a miss, and which server handles each request?
Press Run it, then step through and watch the requests move.
What the counters show at the end:
| Count | Time | |
|---|---|---|
| Creates | 3 database writes | 3 × 10 = 30 ms |
| Redirects | 5 hits, 3 misses, so 3 database reads | 5 × 1 + 3 × 11 = 38 ms |
| Servers | app1 6 requests, app2 5 |
Each code misses once, on its first redirect, and is a hit after that. Without the cache, all eight redirects would read the database: 8 × 10 = 80 ms.
How big a cache?
Not every link is popular. Say the 10 million busiest links get almost all the clicks. At 500 bytes a link, that is 10,000,000 × 500 = 5,000,000,000 bytes, or 5 GB. A cache also uses some memory of its own for each pair it keeps, so the real figure is somewhat higher, but it still fits in the memory of one cache server. The share of reads the cache answers is the . At 0.9, only the misses, one redirect in ten, reach the database: about 386 a second instead of 3,858.
At a hit ratio of 0.99 the database sees only about 39 reads a second. In Java, hotLinks is a long, because 10 million × 500 is 5 billion, more than the largest int.
Copies for the rest
The misses still need a database. The Databases at scale tutorial shared reads across copies. A is a copy of the database that takes every change from the primary and answers reads. The 38.6 writes a second go to the primary; the misses can be shared across the replicas.
A replica can lag a little behind the primary. A link made a moment ago may not be on the replica yet, so a redirect that finds no row there can ask the primary before it answers 404.
Check yourself
In the flow, link 21 is made first. A moment later the first redirect of 21 is a miss. Why?
The service gets 3,858 redirects a second. If the hit ratio is 0.8 instead of 0.9, about how many redirects a second reach the database?
Say the 20 million busiest links get almost all the clicks. At 500 bytes a link, how much memory does the cache need to hold them?
Honesty notes
- Serve redirects cache-aside: look in the cache, and on a miss read the database and fill the cache.
- Read the flow: three creates and eight redirects give 5 hits, 3 misses, 3 database writes and 3 reads, 38 ms of redirects and 30 ms of creates.
- Size the cache: 10 million busy links at 500 bytes is 5 GB, and a hit ratio of 0.9 leaves about 386 database reads a second.
- Share the remaining reads across read replicas, and ask the primary when a brand-new link is not on a replica yet.