Skip to content

Design a URL Shortener, stage 6 of 9: decide

Count clicks without slowing redirects

Customers want clicks per link, per day, per country. A popular link may get 5,000 clicks a second during a campaign. Counts may lag by a minute or two.

System so far· 6 parts
12345CLIENTClickersEDGECDN edgeSERVICERedirect serviceDATABASEPostgresSERVICELinks APICLIENTCustomerdashboard

Select a component to see what it is responsible for and which state it owns.

  1. 1Clickers → CDN edge: GET /aZ3kQ9x
  2. 2CDN edge → Redirect service: Cache miss
  3. 3Redirect service → Postgres: Look up code
  4. 4Customer dashboard → Links API: Create, edit, disable
  5. 5Links API → Postgres: Insert with unique code

What you need to know

0 of 3 checks done
  1. The obvious approach is to update a counter during each redirect:

    UPDATE links SET clicks = clicks + 1 WHERE code = 'aZ3kQ9x';

    An UPDATE locks the row until its transaction commits, so two updates to the same row take turns.

  2. Think first

    A link gets 5,000 clicks a second, and every click runs that UPDATE on the same row. What happens to redirects for that link?