System Design Basics
Design URL ShortenerMediumWrite Code
1/10
Hashing + DB

Description

Design a URL shortening service like bit.ly. You need to outline the core components: how short URLs are generated, how they map back to original URLs, and how the system handles high read traffic. Focus on the data model, the hashing/encoding strategy, and how you'd scale reads.

Requirements

01Define the database schema (table name, columns, indexes)
02Explain the short-code generation strategy (base62 encoding, counter vs hash)
03Describe the read path: what happens when a user hits `short.ly/abc123`
04Add a caching layer for hot URLs — explain cache invalidation strategy
05Discuss how you'd handle collisions if using hashing

Example

# A strong answer covers:
# - Table: urls (id BIGINT PK, short_code VARCHAR UNIQUE INDEX, original_url TEXT, created_at TIMESTAMP)
# - Base62 encoding of auto-increment ID (no collisions, predictable)
# - GET /:code → check Redis cache → fallback to DB → 302 redirect
# - Redis cache with TTL 24h for hot URLs, write-through on creation
# - Horizontal scaling: read replicas for DB, Redis cluster for cache
Python 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Ln 33, Col 1