System design round

Design a URL Shortener

The classic first system design question. Writing links is easy; the design is about redirects, which outnumber new links a hundred to one, and about counting clicks without slowing them down.

Estimate, draw your design, run its traffic through it and see which boxes break. Then answer the follow-up questions. The load test is free and needs no account; an AI review or score uses one of your AI replies.

The brief

It must

  • Create a short link for a long URL, with an optional custom alias and expiry.
  • Anyone who opens a short link is redirected to the long URL, quickly.
  • Count clicks per link per day.
  • Codes never collide, and cannot be guessed one after another.

The numbers

  • Assume 500 million new links a month.
  • Each link is opened about 200 times in its life.
  • The busiest hour carries three times the average traffic.

Before you draw: the numbers

Interviewers expect a quick estimate first, because the numbers decide the design. Write yours, then check.

The load test uses round numbers per box, the kind you would use on a whiteboard, so it shows where a design breaks and roughly by how much, not exact figures. How it works

The follow-up questions

Once your design is up, the interviewer goes deeper. Each question changes one thing and is a level further, Fresher to Senior. An AI interviewer scores your answer; 6 out of 10 or more climbs the rung.

URL shortener

Not climbed yet

Our design, and why

Try your own first. “Our design” on the board shows it as boxes you can load-test.

Start from the numbers: about 116,000 redirects and 600 new links a second at the peak. Everything about this design follows from reads outnumbering writes 200 to 1.

  • Link service behind a load balancer: stateless machines, so it grows by adding more. Twelve handle the share the CDN does not answer.
  • Redis in front of Postgres: a code maps to a URL that almost never changes, so the cache answers about 95% of lookups and the database sees a few thousand reads a second. A replica keeps the cache up when a node dies; without it, every lookup lands on Postgres at once.
  • A CDN caches the redirect itself for popular links. It is the cheapest capacity there is, at the price of seeing fewer clicks yourself: decide between 301 and 302 knowing that.
  • Clicks go to Kafka, not to the database inside the redirect. Workers count them in memory and write totals per link per day in batches, a hundred times fewer writes. Losing a few events in a crash is acceptable for analytics, which is why they can leave the critical path.

Codes: hand each service machine a range of ids from a counter and write them in base 62 (seven characters give 3.5 trillion codes), shuffled so they cannot be walked in order. Or generate random codes and let a unique constraint catch the rare collision.

What each level is expected to cover

The AI review grades your board against these.

Fresher
  • Two endpoints: create a link, and redirect on GET /{code}.
  • A links table keyed by the code.
  • Notices that redirects far outnumber new links.
SDE 1
  • Codes that cannot collide: counter ranges in base 62, or random codes with a unique constraint.
  • A cache in front of the database for redirects.
  • Stateless app servers behind a load balancer.
SDE 2
  • Click events taken off the redirect path through a queue, and aggregated.
  • CDN or edge caching of redirects, and the 301 versus 302 trade-off.
  • Read replicas or sharding by code, sized from the numbers.
Senior
  • What happens when the cache fails (stampede onto the database) and how to survive it.
  • Abuse: phishing links, rate limits on creation.
  • Analytics details: bots, late events, exactly what is counted when the CDN answers.

How the load test works

Each round sends a few kinds of traffic from its users, at the rates in the brief. The test follows your arrows: a CDN answers what it can cache, a load balancer spreads requests, a service runs the logic and checks a cache beside it before the database, and a queue takes work off the request so the boxes after it work at their own pace.

Every box takes a rough number of requests a second (one service machine about 5,000, one Redis node about 100,000, one SQL primary about 3,000 writes, one row about 200 updates). A box asked for more turns red and drops the excess, unless a queue in front holds it back, in which case the queue grows and the work waits. Failure scenarios take machines away; a standby copy covers one.

It cannot see correctness: idempotency, consistency, and what users see when something fails. Write those in a box’s note; the AI review reads them.

blur