LESSON 06 · Operate and protect

Plan for failure and misuse

A redirect service must remain trustworthy when dependencies fail or attackers create links.

SYSTEM VIEW

The system

Diagram view
DB reads—
Redirects—
Click events—

Watch redirects ride out cache and storage failures, then see creation guarded and analytics kept off the redirect path.

  1. At 8,700 redirects a second and 90% cache hits, the Mapping database serves about 870 reads a second. One of the Mapping cache’s two nodes fails. Its keys miss, the Redirect service falls back to the database, and reads jump to about 4,800 a second.

    Before
    about 870 reads/s

    What changed

    • 1 of 2 cache nodes
    • DB reads 870 → 4,800/s
  2. Request coalescing lets concurrent misses for one key share a single database read: three visitors miss x7Kp2Qa together, and one SELECT answers all three. A cap on concurrent queries bounds what reaches the database, so the surge doesn’t become an outage.

    What changed

    • 3 misses share 1 read
    • Cap on concurrent queries
  3. Both cache nodes are up, but the Mapping database is down. Hot keys still redirect from the cache for a while. A new link can’t be stored, so the Create service returns an error, not 201: creation doesn’t claim success until its mapping is durable.

    201 promises
    a durable mapping

    What changed

    • Both cache nodes up
    • Mapping database
    • Error instead of 201
  4. Monitoring tracks five signals: error rate, redirect latency, cache hit rate, database pressure and key-generation conflicts. The lost cache node showed as a falling hit rate and rising pressure; this outage shows as rising errors. Monitoring is part of the service, not optional polish.

    Both paths
    error rate · DB pressure
    Read path
    latency · hit rate
    Creation
    key conflicts

    What changed

    • Monitoring
    • Error rate up
  5. Storage is back, and creation now passes through the URL policy. It accepts http and https, refuses javascript: links, and checks destinations against a known-bad list such as Google’s Safe Browsing. A link can turn malicious later, so stored links are rechecked periodically.

    Allowed
    http, https
    Known-bad list
    e.g. Safe Browsing

    What changed

    • Storage back up
    • URL policy
    • javascript: link
  6. A script posts thousands of links at once. The Rate limiter now stands before the URL policy and lets only a steady trickle through, so the burst can’t flood creation. Together with the scheme check, it stops a javascript: spam run.

    What changed

    • Rate limiter
    • Burst held to a trickle
  7. A crafted URL is plain http, so it passes the policy and reaches the Create service. Fetching it there would call an internal address such as Cloud metadata: server-side request forgery, or SSRF. Privileged servers must not fetch submitted URLs without SSRF defenses.

    What changed

    • Crafted URL passes
    • A fetch would reach metadata
  8. Each redirect publishes a click event to the Analytics queue without waiting for it. With the queue down, Best effort keeps redirects flowing but loses or delays some events. Billing needs a durable local buffer, which keeps them and adds latency.

    What changed

    • Analytics queue
    • Clicks published async
    • Queue down
    Click events

Space plays · ← → step · Home resets

THE IDEA

How it works

If a cache node fails, redirect servers fall back to the database, so protect it with capacity and controlled retry behavior. Using the earlier example, losing one of two cache nodes at a 90% hit rate raises database reads from about 870 to about 4,800 per second. Request coalescing, where concurrent misses for one key share a single database read, and a cap on concurrent queries keep that surge from becoming an outage.

If durable storage is unavailable, existing hot keys may still redirect from cache for a limited time, but creation should not claim success until its mapping is durable. Monitor error rate, redirect latency, cache hit rate, database pressure, and key-generation conflicts.

Validate destination schemes such as http and https and reject unsafe ones such as javascript:. Rate-limit creation, and have a policy for spam, phishing, and malicious destinations. Google’s Safe Browsing API can flag known bad URLs, though a link can turn malicious after creation, so recheck periodically. Do not fetch submitted URLs from privileged infrastructure without defenses against server-side request forgery (SSRF), where a crafted URL makes your own server call an internal address such as a cloud metadata endpoint.

If click analytics is needed, publish an event asynchronously; a failed analytics sink should not usually hold up the redirect. The cost is some lost or delayed events. Reporting tied to billing may not accept that, so it needs a durable local buffer and the extra latency that brings.

DECISION POINT

An analytics queue is unavailable while people click existing links. Should redirects wait for it?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Separate durability of new links from availability of cached redirects.
  • Abuse controls and observability are part of the service, not optional polish.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

After a database outage, some links created during it return 404, though the API answered 201 for each. What went wrong?

Choose an answer. You’ll see the explanation once you get it right.

Design a code-snippet sharing service

Move from short links to a product where the stored value is large, most of it expires, and some links must stay secret.

The brief

Developers paste code or logs up to 1 MB, choose an expiry, and get a link. A public link can be shared widely and cached. A private link must be impractical to guess, and it must stop working when it expires or its owner revokes it. Design the first version. Keep full user accounts, comments, version history, and search outside this exercise.

  • 2 million new snippets are created per day, and each is read 5 times on average. Peak traffic is 10 times the daily average. Use decimal units and 86,400 seconds per day.
  • A snippet averages 20 kB, with a hard limit of 1 MB.
  • Half of snippets expire within a day, 40% expire after 30 days, and 10% never expire. Estimate stored text after one year of steady traffic, before replicas and metadata.
  • Size keys for five years of creation. Assume an attacker can make 1,000 guesses per second against private links.
  • A private link must stop serving within 60 seconds of expiry or revocation. An accepted snippet must survive an application-process crash.

Reading a design isn’t the same as producing one under a clock. Try it timed: the requirement change stays hidden until partway through.

Loading your draft…

State your scope and assumptions. Estimate average and peak create and read rates, bytes written and served per day, stored text after one year, and the key length you need. Keep units visible.

0 / 4,000 characters

Name the components and what each stores. Trace one create, one public read, and one private read, including where expiry is checked and where snippet bytes live. An arrow list is enough.

0 / 4,000 characters

Choose a likely bottleneck and the metric that would prove it. Explain what happens if the cleanup job stops for a day, and how a revoked link stops being served within 60 seconds, given any caches you added.

0 / 4,000 characters

Respond to the burn-after-reading change below. Explain how exactly one reader gets the content, how automatic link previews are handled, and what a crash costs.

The requirement changesA customer asks for burn-after-reading links: a snippet is shown to one reader and is then gone. Chat apps and email scanners often fetch links automatically to build previews. What changes in your read path and storage? State what happens if the server crashes just after a snippet is claimed.

0 / 4,000 characters

Write an initial response in all four sections to unlock the checklist and two worked approaches.

0 of 3 checkpoints answered correctly to complete this lesson.