the systems should be able to precisely count the views of each video.
the system should be able to determine illegitimate view.
the system should be able to update view counts in real time.
Non-Functional Requirements:
Availability
high availability: 99.99% up time.
Scalability
able to support horizontal scaling to handle high traffic volume spike with minimal failure
Consistency
Data stays consistent across database.
Latency
Low response time: real time response < 500 ms
API Design
External API
1.Retrieve Video
METHOD: GET
Endpoint: /videos/{video_id}
Path Variables:
video_id: str: id of video
Response
Success:
status: ENUM: SUCCESS
status_code: int: 200
video: blob: the video itself
video_metadata: object: the metadata of the video
Video not found
status: ENUM: ERROR
status_code: int: 404
message: str: video not found. It might be deleted or limited access
Bad service
status: ENUM: ERROR
status_code: int: 503
message: str: log that can be used for root cause analysis
Internal API
1.Increase View
METHOD: PATCH
Endpoint: /increase_view/{video_id}
Path Variables:
video_id: str: id of video
Body:
views: int: number of view increase
Response
Success
status: ENUM: SUCCESS
status_code: 200
Video not found
status: ENUM: ERROR
status_code: int: 404
message: str: Video not found. It is possible that it might be deleted or limited
Access denied
status: ENUM: ERROR
status_code: int: 403
message: str: no permission allowViewUpdate for update video
High-Level Design
Database Design
system nature
in this system we only needs to map the video with its view
hence there is only 2 columns that needs to be kept together
Choices
SQL database
database such as mysql can be used to deal this type of problem by storing video_id, and view count in table Views
video_id will act as primary key while views_count will behave like one of the usual column
noSQL database
in this use case we can also apply noSQL database, to be more specific key-value database.
We only need to keep video_id as key and views_count as value
Decision
In this case, the design would benefit from using key-value database.
While the structure of data is consistent across the system's life sql database is known for its complexity for scaling. Action like sharding is far more complex than its noSQL database
And as there is only 2 columns we can safely use key-value database.
Detailed Component Design
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.