High-Level Design
- Client (user browser, mobile any device)
- CDN
- cache the redirect request -- the short url redirect to the long url
- WAF/bot DDOS attack.
- Block bad lists short url per geo/ bad request.
- Block list of malicious requests
- Api gateway
- Auth - JWT/apikey
- rate limits.
- Forward the right request to the right server.
- request id + validation
- Load balancer
- Distribute the request load to different servers.
- Health check. failover.Rolling deploys
- Redirect Service
- Handle cdn miss cache get request.
- look up from cache, if cache doesnt have it, load from db. If db has, load into cache.
- If no short url record, return 404.
- if short url record disabled return 410
- Redirect service group cache
- KV cache store short (key) url - long url
- TTL 60s
- Manager server
- Handle Post/patch/delete requests
- Generate Id and update records
- write the new short url / long url pair into db
- DB
- KV, short url code (key):
- {
- long url :string ,
- create_at: timestamp,
- expired_date: timestamp,
- disabled: boolean
- }
- Async pipeline:
- for analysis -- fetch the visit log from cnd
- update expired data to be 90days from today when a record is visited today.
Read Flow:
- Client sends get request
- CDN: it arrives at cdn first. And 99% visit should be stopped and redirect successfully at the cdn part. if the short url found, the return 301/302
- redirect server: if cdn cache missed, cdn forward to redirect server, where it reads from cache see if the long url exist and then db. If both not exist, return 404/410.
- This can handle 10M qps, thanks to the cdn will handle 99% requests.
Write Flow:
- Client sends post/patch/delete requests
- Api gateway forwards to manage service
- Load balancer will handle the load
- Manage service will handle create the short url code and generate the code id
- Manager service will handle delete the record by update db
- Manager service will handle disable the record by update db
The short url is combine: domain/{code}
In the manager server: we need to generate code from the long url path.