URL Shortener System Design: Architecture, Redirects, IDs and Scaling
A shortener is a write path for link creation and a high-volume read path for redirects.URL shortener system design is a useful exercise because it combines key generation, storage, caching, low-latency reads, analytics and abuse prevention. The core job is simple: map a short code to a destination URL and redirect visitors reliably.
1. Requirements
Functional requirements: create a short link, resolve a code, optionally support custom aliases and expiration, and provide link management. Non-functional requirements: low redirect latency, high availability, collision resistance, secure validation, observability and predictable data retention.
2. High-level architecture
Client
| POST /api/links
v
API service ----> URL validation
| |
v v
ID generator ------> Database
^
|
Visitor --> Redirect service --> Cache --> Destination
|
+--> Async event queue --> Analytics store
Separate link creation from redirect resolution. Redirects are typically the latency-sensitive path; analytics events should not make the visitor wait for a slow reporting query.
3. API design
| Endpoint | Purpose | Example response |
|---|---|---|
POST /api/links | Create a short link | Short code and destination metadata |
GET /:code | Resolve a short code | HTTP redirect to destination |
GET /api/links/:id | Read link details | Owner-authorized link record |
PATCH /api/links/:id | Update supported fields | Updated link record |
Validate request bodies, normalize URLs carefully and require authentication for owner-only management endpoints. Public redirect resolution should not expose private account data.
4. Short-code generation
Options include random tokens, database-generated numeric IDs encoded in Base62, or distributed ID generators. Random tokens need collision checks and retry logic. Sequential IDs are compact but can be predictable; an encoding step is not encryption. Choose based on scale, predictability, uniqueness and operational complexity.
5. Data model
links
- id
- code (unique index)
- destination_url
- owner_id
- created_at
- expires_at
- status
Use a unique constraint on the short code. Add indexes for owner and expiration queries according to access patterns. Keep analytics events in a separate store or event pipeline rather than growing the hot link table with every click.
6. Redirect and cache strategy
Resolve the code from cache first, then the database on a miss. Cache entries need a policy for updates, deletion and expiration. Decide whether the product uses 301 or 302 redirects based on whether the destination is permanent or may change; browser and intermediary caching behavior should be considered before choosing.
7. Analytics pipeline
Record events asynchronously where possible. Store timestamps, short-link identifiers, referrer information and coarse device or country attributes only when lawful and disclosed. Avoid collecting unnecessary personal data. Bot filtering and unique-click definitions must be explicit because a request is not necessarily a human visitor.
8. Security and reliability
- Validate schemes and reject unsafe protocols.
- Protect creation APIs with authentication, rate limits and abuse controls.
- Detect phishing, malware and spam destinations using an appropriate review process.
- Prevent open-redirect abuse in management endpoints.
- Use backups, monitoring, structured logs and tested recovery procedures.
- Keep secrets out of source code and logs.
Frequently asked questions
Which database is best for a URL shortener?
There is no single best database. Choose based on expected scale, consistency needs, query patterns, operational expertise and durability requirements.
Should redirects use a cache?
A cache can reduce repeated database reads for popular links. It adds invalidation and expiration complexity, so measure traffic patterns and design cache behavior deliberately.
Is Base62 enough to make IDs secure?
No. Base62 is an encoding alphabet, not a security mechanism. Predictability and authorization must be addressed separately.
Should analytics block redirects?
Usually avoid making a redirect depend on a slow analytics write. An asynchronous event pipeline can improve response latency, with clear handling for event loss and retries.
Conclusion
A scalable URL shortener needs more than a code generator. Design the redirect path for low latency, enforce uniqueness, separate analytics from resolution, plan cache invalidation and build abuse prevention into the service from the start.