URL Shortener (TinyURL) 00:00:01 Create one durable mapping One person pastes a long address and another opens the short link three months later. They share almost no work except one critical fact. That short code still needs to identify the intended destination. POST /api/v1/urls with the long URL and an optional alias and expiry. It captures what the user wants before any work. The gateway passes the request on. It owns cross-cutting work (auth, limits) and the service owns the business logic. The service uses its own range. Only an empty range needs ZooKeeper. No network call per link. Plain base62 turns 1234567 into 5ban. In production the ID is scrambled first, so neighbours get unrelated 7-character codes. Short, URL-safe, hard to guess. Put the mapping in Redis with a 24 hour TTL, without waiting. New links are often clicked straight away. About 150 ms in total. The user can copy and share it. This is an illustrative design. Reserve a user-selected alias atomically. Checking whether it exists and only later deciding to write would let two customers believe they owned the same public address. 00:01:27 Follow an old link A browser sends GET sho.rt/5ban from an email, a post or a QR code. Every millisecond here is felt. About 35% of requests go on to the nearest healthy region. The edge already absorbed the rest. The service asks Redis for the short code. This illustrated warm-cache lookup takes about a millisecond, which is a workload assumption. The service publishes a compact click event asynchronously. Define and monitor the acceptable analytics loss if the fast redirect can finish before delivery is confirmed. The reply passes the balancer to the CDN. The browser opens the long URL about 70 ms after the click. Choose redirect and cache behavior according to whether the owner can edit the target. A redirect that a browser has already cached may outlive an update the service itself has correctly stored. 00:02:25 Encode a number into a code Use the digits zero through nine, then lowercase A to Z, then uppercase A to Z. Repeated division by sixty-two will identify each character. Start with the unique number one million two hundred thirty-four thousand five hundred sixty-seven. Divide by sixty-two. The quotient is nineteen thousand nine hundred twelve and the remainder is twenty-three. In this alphabet index twenty-three is lowercase N, the final character. Divide nineteen thousand nine hundred twelve by sixty-two. The quotient is three hundred twenty-one and the remainder is ten. Index ten is lowercase A. Divide three hundred twenty-one by sixty-two. The quotient is five and the remainder is eleven, which gives lowercase B. Five divided by sixty-two has quotient zero and remainder five. Its character is the digit five. Read the remainders in reverse to get the short code. Check the result. Start with five, multiply by sixty-two and add eleven to get three hundred twenty-one. Multiply that result by sixty-two and add ten to get nineteen thousand nine hundred twelve. Multiply once more by sixty-two and add twenty-three. The answer is one million two hundred thirty-four thousand five hundred sixty-seven. Base sixty-two changes the representation of the allocated number. It gives a compact alphabet, but it cannot make two independently allocated copies of the same number unique or make predictable identifiers secret. 00:04:04 Count without delaying the visitor Ten billion clicks a day, counted without slowing one redirect. The redirect is already on its way when the event is sent: about 100 bytes. Partitioned by code, kept for 7 days. If consumers stop, clicks wait here. The processor drops repeated event IDs and groups by code, country, referrer and device. 1,500 events became one row: US 1,000 and UK 500; Twitter 800 and direct 700. The dashboard reads hourly rollups for one code, so it scans few rows and answers in under 100 ms. Asynchronous counting protects redirect latency. Define which action counts as a visit and how duplicate or lost events affect the dashboard before promising precise analytics. 00:04:54 Lose the shared cache Three failures in a row. The service stops calling Redis and reads DynamoDB directly. Latency rises, links still work. That is 7 times the normal reads: raise the edge TTL, use in-process caches and shed the least important traffic. Redis returns. Warm it from access logs, then close the circuit slowly. A hot destination can send thousands of misses to one database row. Preserve a cached response where its contract permits, cap origin traffic and track how long redirects actually take. 00:05:28 Pause the allocator No new ranges can be leased. How long do writes survive? Servers cannot reach ZooKeeper. Nothing user-visible happens. Ten servers at 115 links a second each: a million IDs last about 2.4 hours. Servers lease new ranges. Reads were never involved. Leasing ranges in advance allows a writer to keep creating codes until its local range is exhausted. An existing short code requires a mapping lookup and does not need a freshly allocated number. Keep the redirect alive independently of analytics and of allocating new codes. The stored mapping is what makes an old piece of printed paper useful months later.