Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
I would use a stateless API behind a load balancer, a database for the URL mapping, Redis for frequently accessed links, and a scalable ID-generation strategy such as Base62 encoding. Redirects should be optimized for low latency, while analytics can be processed asynchronously.
I would begin by clarifying the requirements. The core operations are creating a short URL and redirecting a short URL to its original destination. The system should provide unique short codes, high availability, low redirect latency, and enough durability that links do not disappear unexpectedly.
A basic architecture could look like this:
Client
|
Load Balancer
|
Stateless API
|-------- Redis
|
Database
|
Message Queue ---> Analytics WorkersFor short-code generation, one option is a globally unique numeric ID encoded using Base62. Another option is a distributed ID generator. Random codes can also work, but collisions must be detected and handled.
The redirect path is read-heavy, so Redis can cache popular URL mappings. On a cache miss, the API reads the database and populates the cache. The database should have an index on the short code.
At larger scale, the database can use replication and partitioning depending on traffic and data size. The API itself should remain stateless so that requests can be distributed across instances.
Analytics should not block redirects. Click events can be published to a queue and processed asynchronously. This separates the critical redirect path from potentially expensive analytics processing.
Important failure scenarios include Redis outages, database failures, duplicate short-code generation, hot URLs, and abuse such as malicious links. A production design would also need rate limiting, abuse detection, monitoring, and backup/recovery procedures.