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
Select a component to see what it is responsible for and which state it owns.
- 1Clickers → CDN edge: GET /aZ3kQ9x
- 2CDN edge → Redirect service: Cache miss
- 3Redirect service → Postgres: Look up code
- 4Customer dashboard → Links API: Create, edit, disable
- 5Links API → Postgres: Insert with unique code
What you need to know
0 of 3 checks done
The obvious approach is to update a counter during each redirect:
UPDATE links SET clicks = clicks + 1 WHERE code = 'aZ3kQ9x';An
UPDATElocks the row until its transaction commits, so two updates to the same row take turns.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?The fix has two parts:
- Don't wait. The redirect appends a click event to a durable stream (like Kafka or Kinesis) and responds immediately. See Asynchronous processing.
- Batch. A separate worker reads events in batches, adds them up per link, day and country, and writes one total per group.
Work it out
The worker flushes every 10 seconds. For the link getting 5,000 clicks a second, how many clicks does each write now cover?Check
During a huge campaign the worker falls behind and the stream builds a backlog. What do users notice?One catch: if the CDN answers a redirect from its cache, the request never reaches your redirect service, so no event is emitted. With edge caching, exact counts have to come from the CDN's logs or from code running on the CDN.