Q: How many URLs are generated per second? 200
Storage: 200 * 60 * 60 * 24 * 365 * 5 * (200 Byte) = 6.3 TB
Q: How many URLs are redirected per second?
20,000
Query rate: 20,000 URLs / s
Bandwidth:
shorten: 200 * 200Byte * 8 bits = 320 Kbps
redirect: 20000 * 200Byte * 8bits =32 Mbps
Cache Memory: cache 20% daily
20000 * 60 * 60 * 24 * (200 Byte) * 20% = 69 GB
Servers needed at peak load
20000 * 5000 / 64000 RPS = 1562 servers
Response: short_url
Response: original_url
Store
MongoDB
User
userId(20 Byte) Primary Key
user_name(20 Byte)
createTime (10 Byte)
url
short_url(50Byte) Primary Key
user_id(20Byte) Secondary index
original_url(200 Byte)
creation_time(10 Byte) Secondary index
expiry_time(10 Byte) SecondaryKey
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
sequenceDiagram
participant Client
participant APIGateway
participant AuthService
participant ShortenURLServer
participant Cache
participant Database
participant URLGenerator
Client->>APIGateway: POST {long_url}
APIGateway->>AuthService: Check Auth
AuthService-->>APIGateway: Auth Success
APIGateway->>ShortenURLServer: Forward Request
ShortenURLServer->>Cache: Check Cache for {long_url}
Cache-->>ShortenURLServer: Miss (No Result)
ShortenURLServer->>Database: Check Database for {long_url}
alt Long URL exists
Database-->>ShortenURLServer: Found {short_url}
ShortenURLServer-->>Client: Return {short_url}
else Long URL does not exist
Database-->>ShortenURLServer: Not Found
ShortenURLServer->>URLGenerator: Generate new {short_url}
URLGenerator-->>ShortenURLServer: New {short_url}
ShortenURLServer->>Database: Store {long_url, short_url}
ShortenURLServer->>Cache: Optionally Cache {long_url, short_url}
ShortenURLServer-->>Client: Return new {short_url}
end
sequenceDiagram participant Client participant APIGateway participant AuthService participant RedirectURLServer participant Cache participant Database participant URLGenerator Client->>APIGateway: GET {short_url} APIGateway->>AuthService: Check Auth (OAuth) AuthService-->>APIGateway: Auth Success APIGateway->>RedirectURLServer: Forward Request RedirectURLServer->>Cache: Check Cache for {short_url} alt Short URL in Cache and Valid Cache-->>RedirectURLServer: Found and Valid RedirectURLServer-->>Client: Return 301 Redirect to Original URL else Short URL not in Cache or Expired/Deleted Cache-->>RedirectURLServer: Not Found RedirectURLServer->>Database: Check Database for {short_url} alt Short URL in Database Database-->>RedirectURLServer: Found RedirectURLServer-->>Client: Return 301 Redirect to Original URL RedirectURLServer->>Cache: Optionally Cache {short_url} else Short URL not in Database Database-->>RedirectURLServer: Not Found RedirectURLServer->>URLGenerator: Generate new {short_url} URLGenerator-->>RedirectURLServer: New {short_url} RedirectURLServer->>Database: Store {long_url, short_url} RedirectURLServer->>Cache: Optionally Cache new {short_url} RedirectURLServer-->>Client: Return New {short_url} end end
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Database: MongoDB, because the system is read heavily and the data is unstructured. atomatically when write.
Message queue like Kafka for the invalidate cache, because the async protocol and eventual consistency.
we can add read limiter to limit the number of requests for the short-term reduce the query
and add cache for all levels, local, DNS, webserver, apiserver, database to avoid the database hit and improve the latency.
For the long term, we can add more server, use the database sharding and database geologically distribute to improve the database query efficiency.