System Design: Design tiktok/reels

Functional requirements:

  • user should be able to upload videos

  • user should be able to view/consume videos

  • user should be able to view other user's profile

Non-functional requirements:

  • Eventual consistency ( it is okay for a video to be eventually available across nodes)

  • Low latency in serving videos

  • Fault tolerant, high availability

  • Scalability

Additional requirements:

  • User should be able to like/comment/react to a video

  • User should be able to save a video

API contracts:

uploadVideo(userid,timestamp,videometadata,videofile)

viewVideo(videometadata,userid)

viewUser(userid)

Data Model:

Userdata: User data has a fixed schema and we needn't change the schema in future. Therefore, we can go with Relational database such as Postgres, MY SQL or Oracle.

Video metadata: Schema needs flexibility because we may add new attributes to video in future. Some attributes may be applicable to a particular video. Therefore, a key-value store would be ideal for storing video metadata.

Video files: We need a blob storage. A combination of Amazon S3+ CDN would be idea to persist videos. CDN would make sure these videos can be served across geographies with minimum latency. Ideally, CDN will also include userdata and video metadata. For low latency, we can shard this database with country/location id as key. This will reduce the latency when user consumes the videos of his/her geography

Capacity estimation:

  1. Active users per day: 2 million
  2. Ratio of writes to reads: 1:10
  3. Number of video uploaded: 200k
  4. Number of formats per video: 3
  5. Number of resolutions per format: 2 (size of the file decreases as resolution decreases, so a multiplication factor of 2 can be assumed for 4 resolutions per video)
  6. Avg size of a video: 1 MB
  7. Data to be persisted per day: 200k X 3 X 2 X 1 MB = 1200 GB = 1.2 TB

Architecture:

architecture.JPG

Network protocols:

For video upload and consumptions, we can use HTTPS. HTTPS is built on top of TCP, which ensures guarantee and sends the data packets in orderly manner.