~learn.
0:00 / 1:35
W01 · Data at scale

A 404 for a short link that exists

transcript

You create a short link. You send it to a friend. They tap it. 404. The link exists. So what happened?

Each partition has a primary and read replicas. Creates go to the primary. Redirects go to a replica. That spreads the read load. But replication is async. The replica runs about 50 milliseconds behind.

Zero milliseconds. abc123 is written to the primary. You share it at once. 3 milliseconds in, your friend clicks. The read lands on the replica. The replica has never heard of abc123. 404. At 50 milliseconds it catches up. Too late. Your friend already saw it broken.

Here's the twist. It's a read your writes bug. But for someone else's write. Lag is usually under 100 milliseconds. Unlikely. But possible.

Fix one: wait for a replica on every write. About a millisecond more. But async replicas can still miss. Fix two: read brand-new links from the primary for 5 seconds. More primary load. Fix three: write the link into the cache when it's created. Not at first read.

The lesson picks three. Redis is already there. Now the friend's click hits the cache, never the lagging replica.

Replicas are always a little behind. Plan for the click that beats them. The link is cached before you even finish sharing it.

Full breakdown in the lecture.