पाठ 29 / 30

Worked Interview Problem: A URL Shortener

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Walk through a full estimate for a URL shortener the way an interviewer expects to hear it.

लेखक: · 15 साल से वेब डेवलपर

Setting up the problem

Interviewers often open with 'estimate the scale of a URL shortener'. Clarify: core actions are creating a short link (write) and redirecting (read); analytics are out of scope. Assume: 100 million new links per month; a read-to-write ratio of 100:1; each record (short code, long URL, creation time, owner) about 500 bytes; keep links for 5 years; peak factor 3. Traffic: 10^8 / (2.5 x 10^6 s) is about 40 writes per second on average, so about 4,000 redirects per second, and roughly 12,000 at peak. Storage: 10^8 x 12 x 5 = 6 x 10^9 links x 500 B = 3 TB logical, perhaps around 10 TB with replication and overhead. Key space: 6 billion codes need a short code alphabet large enough; base-62 with 7 characters gives about 3.5 x 10^12 codes, ample. Conclusion: reads dominate, so cache hot links; the dataset fits a modest sharded or replicated store.

Checking the numbers

Verifying the mental arithmetic from the walkthrough.

links_per_month = 100_000_000
month_s = 30 * 86_400
write_qps = links_per_month / month_s
read_qps = write_qps * 100
total_links = links_per_month * 12 * 5
print(f"writes ~ {write_qps:.0f}/s, reads ~ {read_qps:,.0f}/s, peak reads ~ {read_qps * 3:,.0f}/s")
print(f"links in 5 years: {total_links:.1e}, logical size: {total_links * 500 / 1e12:.0f} TB")
print(f"base62 7-char codes: {62 ** 7:.2e}")

Output:

writes ~ 39/s, reads ~ 3,858/s, peak reads ~ 11,574/s
links in 5 years: 6.0e+09, logical size: 3 TB
base62 7-char codes: 3.52e+12

Narrate, then compute

Say what you are about to compute and why before writing numbers. Round aggressively, state the bottleneck at the end, and connect each number to a design decision such as 'so a cache in front of the store is worthwhile'.

Explaining the route before driving

A good auto driver tells you the route before starting: main road, then flyover, then the market lane. If you object to the flyover, you change one step. Narrating an estimate gives the interviewer the same chance to adjust a single assumption.

त्वरित जाँच: In the URL shortener walkthrough, what is the main design conclusion from the numbers?

  • The system is write-heavy and needs many write shards
  • The dataset is too large to store on disks
  • Reads dominate, so caching popular links pays off and storage is modest
  • A 4-character code is enough for 5 years
Answer

Reads dominate, so caching popular links pays off and storage is modest — With 100:1 reads to writes and only a few terabytes of logical data, the read path benefits most from caching. 62^4 is about 14.8 million, far too few codes.