Skip to content

URL shortener · 6 of 6

Step 1 · The pattern

Redirect and count in one go

Each click does two things on one database connection: read where the link goes, then write a row saying it was clicked. Simple, and the count is always exact.

  • click
  • read + write the click
  • redirected

In short

Async click counting: how it works and how it breaks

A redirect needs one thing: the destination for a code. That is a quick read. Counting the click is extra, and it is a write, which costs far more. If the write happens inside the same request, every redirect holds a database connection until the write is done. Connections are limited, so a busy hour fills them, and people time out on a redirect that only needed a read. The fix is to split the two jobs. The redirect looks up the link, drops a small click event onto a queue and answers at once. A background worker takes events off the queue and writes them in batches. If the database slows down, the queue grows and the totals run behind, but links keep working. The price: counts lag slightly, and a queue that delivers at least once can repeat an event, so counting must tolerate duplicates.

How this comes up in interviews

"Your shortener records every click in the database during the redirect. Under a campaign, redirects start timing out. How do you redesign it?"

Then they ask

  1. The queue delivers a click event twice. How do you keep the totals right?
  2. The analytics database is slow for ten minutes. What do visitors see, and what must be true of the queue?
  3. What does the product lose by counting clicks asynchronously?

A strong answer covers

  • The redirect needs one quick read. Counting is a write, costs far more, and holds a database connection while it runs.
  • Connections are limited, so under load redirects queue for a connection and time out because of the counting.
  • Split the jobs: look up the link, drop a click event on a queue, answer at once.
  • A background worker takes events off the queue and writes them in batches.
  • The price: counts lag slightly, and at-least-once delivery can repeat an event, so counting must tolerate duplicates.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.