<
Design a Short URL Service? Not Easy as you Think

Design a Short URL Service? Not Easy as you Think


My colleague discusses his work plan. He needs a feature to create a short link for sending SMS. So I give some questions to know what he needs.

Me: How long maximum character can be?
A: We can put maximum 8 characters

Me: What's the traffic volume?
A: Maybe 1 million url monthly.

Me: Do you have special character allowed?
A: We used only base64 charcter without underscore(_).

Me: What's other feature do you need?
A: We need delete link after expired.

Me: Do you need use same short url, if they are have submitted?
A: No, make it simple and we don't care about update / reuse.

Assuming

From System Design Interview, we know about some points:

  • Flow process: Long URL --> Short URL --> Expired / deleted.
  • Write operation 1 million data & assume read 5x.
  • Assume the average URL length is 50 & expired 1 month. Storage requirements: 50+8 bytes x 1 million = 55 MB/ monthly
  • Need expired feature. The best suite is using TTL feature like on DynamoDB.

High Level Design

API Endpoints

  • POST /shortener (payload: long_url): to create a new short URL.
  • GET /{short_link}: to return / redirect to long URL. Need 302 status code for make "temporarily" moved to the long URL.

What's different status code 301 vs 302:

  • 301 create permanently moved. In this case, you can't do that because you need expired check.
  • 302 create temporarily moved. In this case, you can use it for analytics too & make sure they check the expired link every request.

When you can give maximum 8 characters, we can calculate for collision. Let's assume we use base62, 62 ^ 8 bytes = 218.3 trillion probability. If you need just 1 million, it's more enough to prevent collisions and scaling system.

Using DynamoDB TTL function, it will help to automate cleanup. After time specified time, it will be deleted.

Cost Assumption

Let's calculate again, we use all AWS products for this & cost optimizing without free plan:

  1. AWS Lambda, with RAM 128MB, assuming 50ms/ requests
  2. AWS API Gateway, use HTTP APIs, assuming network out 1KB
  3. AWS Route53, standard
  4. AWS DynamoDB, on-demand capacity
Component Formula Cost
Lambda - Requests 6,000,000req / 1,000,000 x $0.2 $1.20
Lambda - CPU 6,000,000req x 128MB / 1024 x 50ms / 1000 x $0.000016 $0.60
API Gateway - Requests 6,000,000req / 1,000,000 x $1.25 $7.50
API Gateway - Network 6,000,000req x 1 KB / 1024 / 1024 x $0.09 $0.51
Route53 - Hosted Zone 1 x $0.5 $0.50
Route53 - Queries 6,000,000req / 1,000,000 x $0.4 $2.40
DynamoDB - Write WRU 1,000,000req / 1,000,000 x $1.42 $1.42
DynamoDB - Read RRU 6,000,000req / 1,000,000 x $0.29 $1.74
DynamoDB - Storage 1,000,000req x 1KB / 1024 / 1024 x $0.29 $0.28
DynamoDB - Network 5,000,000req x 1KB / 1024 / 1024 x $0.12 $0.57
Total $16.72