Sub topic 15
Availability, failure modes, and trade-offs
This chapter assumes you have read the earlier chapters on the read path, caching, replication, and sharding. You should also know basic HTTP status codes. Here we ask what breaks, what users see when it does, and which trade-offs you accept.
Why the read path sets the availability target
Creating links is rare. Redirects are frequent, and a broken redirect breaks every page that contains the link. So the two paths get different goals. A common target is 99.99% availability for redirects, which allows about 52 minutes of downtime per year. Creation can sit at 99.9%, which allows about 8.8 hours per year.
Failure modes on the read path
Each component in the redirect path can fail. Plan the behavior in advance.
| Failure | Effect | Mitigation |
|---|---|---|
| Cache node down | Every request becomes a miss and the database takes the full load | Replicated cache, and a database sized for a cold cache |
| Cache stampede on a hot code | Many simultaneous misses hit the database for one row | Request coalescing, or a short lock per key |
| Database primary down | Writes fail | Promote a replica, and keep serving reads from replicas |
| Replica lag | A new link returns 404 for a moment | Read from the primary right after creation, or accept brief staleness |
| Whole region down | Total outage | Multi-region deployment with DNS failover |
Degrading gracefully
Return different errors for different problems. A missing code is a 404. An unreachable datastore is a 503, which tells clients and proxies to retry later. If you return 404 during an outage, clients may treat valid links as permanently dead.
class StoreUnavailable(Exception):
pass
def resolve_resilient(short_code: str, cache, db):
long_url = cache.get(short_code)
if long_url is not None:
return 302, long_url
try:
long_url = db.get_long_url(short_code)
except StoreUnavailable:
return 503, None
if long_url is None:
return 404, None
cache.set(short_code, long_url)
return 302, long_urlA cache hit never touches the database, so popular links keep working even while the database is down.
Trade-offs to decide explicitly
- Consistency versus availability. Asynchronous replicas keep reads fast and available, but they can lag. For a URL shortener, a stale read costs little, because mappings rarely change after creation. Choose availability.
- Expiry and caching. A cached entry may outlive the link's
expires_at. Set the cache TTL to the shorter of the default TTL and the time remaining before expiry. - Analytics versus redirect speed. Record clicks asynchronously through a queue. If the queue fails, drop the click event and still redirect. Losing a few counts is better than losing a redirect.
- Cost versus redundancy. Each extra replica or region adds cost. Match redundancy to the target in the first section.
Practices that keep the system recoverable
- Set timeouts on every call to the cache and the database, so a slow dependency does not exhaust your worker threads.
- Use health checks so the load balancer removes unhealthy servers.
- Back up the
urlstable and test restoring it. The mapping is the only data you cannot regenerate. - Monitor the cache hit rate, the p99 redirect latency, and the 5xx rate. A falling hit rate warns of database overload before it happens.
Exercise
The primary database goes down for ten minutes. Which operations should still work, and which status code should a user get when creating a new link?
Show answer
Redirects for cached links keep working. Redirects for uncached links can still be served from read replicas. New link creation fails with 503 Service Unavailable, not 404, so clients know to retry later.