A complete shorten url application
- Table of Contents
- Demo
- Features
- Design
- Limitation
- Redis
- CI/CD
- Kubernetes
- Core-ng
- Spring Boot
- Actix-web
- Authors
Let's play!
- Add shorten url
- IP limiting (5 call for 1 hour)
- Redirect shorten url
Use a distributed counter to encode the url to prevent duplicated url, and use a map to save all encoded and decoded url to fast look up. The whole design is for simplicity only, many other concerns like partitions, high available, CDN and more should be considered before production.
- Non random url
- Single redis can only have 2^32 keys, so may need partitions design
- Single counter may become bottleneck
Suppose we allow 6 characters only, there will be 62^6 (56800235584) possibles combinations for encoded url.
- HashTable for lookup: 62^6 * (2048 + 6) ~= 107 TB (*2048 longest url)
Randomness
- Pre-generated combinations (Need large resource)
- Hash the url (Need conflict resolve)
Scalable
- Use a scalable database instead of redis, better for partitioning, persistent and replication
- A proper setup for CDN can reduce load from your application to CDN network
- Cache for shorten url mapping
- Cache for ip
- Safe incremental counter
I use Github action for CI. Major steps are building java, building image and push to registry.
You can check the image from hokamc/packages/shorten-url-demo
A statefulset for redis and a deployment for app which is easy to scale up.