Failures and trade-offs
Once the diagram is done, the interviewer starts breaking it: "What happens if the store goes down? And the cache? And a whole region?" A good answer to each such question names three things: what keeps working, what the user notices and what you pay for it.
Picture a city water supply. If the pipe to a park fountain bursts, water still runs in the houses: the important consumers do not depend on the minor ones. In our service the important consumer is the redirect, the minor ones are statistics and link creation. The analogy is incomplete: a pipe takes hours to fix, while a store replica takes over from a failed node in seconds.
Why redirects have fewer parts
By the requirements redirects are available 99.99% of the time, about 53 minutes of downtime a year. Link creation is 99.9%, almost nine hours a year.
The availability of a chain is the product of its parts' availabilities. Four parts at 99.99% each give about 99.96% together, so every extra synchronous part eats into the margin. That is why the redirect path holds only the load balancer, the redirect service, the cache and the store: counting is asynchronous, and code allocation is not on it. The parts of this path are themselves duplicated: instances stand behind the load balancer, and the store has three replicas. Each part's availability is well above 99.99%, and on a cache miss a redirect still reaches the store.
Failures one by one
A store node
Each partition has three replicas, and reads move to a live one. Popular links are served by the cache anyway. The user notices nothing, and we pay with triple storage: about 3.6 TB instead of 1.2 TB.
The cache
If the cache is lost, all redirects go to the store while the cache refills. The store's partitions are sized for the peak of 40,000 requests a second, so they hold out for that time, though redirects are slower until the cache refills. The price is store capacity that sits idle on an ordinary day.
The queue or the aggregator
The redirect service sends an event and does not wait for an answer, so redirects keep working. If the aggregator stops, events wait in the queue for up to 7 days. The user notices only that statistics lag. The requirements allow them to lag by an hour, and if the aggregator stays down longer, the numbers arrive late but are not lost.
The code counter
Each link-service instance keeps creating links until it uses up its range of 1,000 codes. At 40 new links a second, that lasts tens of seconds. Then link creation fails, while redirects keep working. For creation this is acceptable: its availability is lower, 99.9%.
A region
Data is copied to a second region asynchronously, and in a disaster DNS moves traffic there. Links created in the last few seconds before the disaster may be lost: that is the price of an asynchronous copy. Each region has its own counter with its own half of the values, so a failover needs no copy of the counter and no code is handed out twice.
Abuse
A failure can also come from outside: a script that creates millions of links will eat codes and space. The limit is at most 100 new links an hour per account and per IP address.
Common mistake
Putting the click-count write on the redirect path "for reliability", so that no click is lost. Then a stop of the counts database takes redirects down too, although the requirements allow statistics to lag. Counting must be asynchronous: losing or delaying an event is cheaper than failing to open a link.
Build the counting path so that this mistake cannot happen.
Redirects must be available 99.99% of the time, while statistics may lag by an hour. There are 4,000 redirects a second on average. Add what gets clicks into the counts without holding up a redirect, and connect the blocks.
Key points
- Redirects need 99.99%, so their path holds only the load balancer, the redirect service, the cache and the store.
- Replicas cover a failed store node, and spare store capacity covers a lost cache.
- A failure of the queue, the aggregator or the code counter does not stop redirects.
- An asynchronous copy in a second region may lose the links created in the last seconds before a disaster.