System Design for short video posts like TikTok, Insta Reel, Youtube Shorts

Purnendu Kar
Nerd For Tech
Published in
6 min readFeb 14, 2023

Now short video content is the new trend, everyone consumes this from platforms like TikTok, Instagram, YouTube etc. Let’s see how we can create a system for TikTok.

recording video on mobile
Photo by Linda Xu on Unsplash

An app like this looks small, but many things are going on in the backend. Following are the challenges associated with it.

  • As the app is used worldwide there will be a lot of requests sent to the server. Which will eventually increase the load on the server.
  • Uploading a video to the backend will be a huge task to perform which will increase the load in the server and block.
  • Streaming the video smoothly without any buffer.
  • And a recommendation system that will recommend videos based on the user’s interest.

Let’s understand the flow one by one. I have divided it into 3 parts:

  • User related service
  • Video post-related service
  • Like & Comment related service
  • Recommendation service

User related service

This is the service that will contain user-related service as mentioned below:

  • Sign up: The user will register themself in the application.
  • Sign in: It will authenticate the credential and send a response to the application.
  • Logout: The user will log out of the application.
  • Follow: If the user wants to follow or unfollow some other user then that could be through this service.

To store user-related data, we would be using SQL-based databases like MYSQL or PostgreSQL as there will be relational data related to the user(like keeping track of followers) so it would be an appropriate option.

To optimise the database performance we will be using master-slave architecture. The master database will be used to perform write operations, and the slave database will be used to perform read operations. To learn more about it you can read How to optimise the database performance and scale it?

Now let’s discuss the flow of the user service. The app will make the API call, and API Gateway will manage the APIs. It will route the request for the user service.

The request will go through the load balancer, under the load balancer there will be multiple user service instances. Based on the load it will decide which instance will process the request. Once the request is been processed load balancer will send back the response to the API gateway and then it send back to the app.

Video post-related service

This is the service that will contain video post-related service as mentioned below:

  • Upload video: Upload video to the backend server.
  • Post: If a user wants to create, edit or delete a post he can do through this service.

To store post-related data, we would be using NoSQL-based databases like MongoDB. For each user, there may be 1000s of posts which will result in a huge amount of data.

It will be hard to scale a database for optimal performance. NoSQL databases support horizontal sharding which helps us to scale the database without compromising performance. To learn more about database sharding you can read All about database sharding | Scaling up the database.

Tiktok video post service

Now let’s discuss the flow of the video service. The app will make the API call, and API Gateway will manage the APIs. It will route the request for the video service.

The request will go through the load balancer, under the load balancer there will be multiple video service instances. Based on the load it will decide which instance will process the request. Once the request is been processed load balancer will send back the response to the API gateway and then it send back to the app.

How to make the file accessible to the whole world without increasing the downloading time?

The video file will be uploaded to a separate cloud storage service like Amazon S3 Bucket. Now if we want to access the file access from anywhere in the world without any latency, now the file will be sent to Content Distributed Network(CDN) which will update the media file to different data cloud storage all over the world.

Can we optimise more to reduce the downloading time?

There is one more challenge that we need to tackle, as the original video could be larger in size, so if we send back a large file to the client then it will take more time to download which will impact the user experience.

Once the file is uploaded to cloud storage you store the file path in the database. And send the post/video details to a Messaging Queue System like Kafka or RabbitMQ.

To make the user experience smooth, we will need to compress the video and make different resolutions videos for different devices. The video processing worker will receive the video details from Messaging Queue System and then it will take the file from the cloud storage and process it. Once the video is processed these new video files will be sent to CDN.

How can we access the compressed video file?

Now you might think how will the application know the file path of the compressed videos that we discussed above? As the compressed will be stored in a categorised folder it would be easy to find the file based on resolution and file name. The video-post API will only return the file name and to access the file app will add the resolution detail in the URL itself like <host>/media/<resolution>/<filename>.

When this URL is hit it will go through the API gateway and fetch the resolution and filename detail from the URL. Then it will check in the cache system(Redis), if the file is not available then it will go for CDN and fetch the file through it. Then it will be added to the cache so that if the same file is requested again then it doesn’t have to fetch from CDN.

Like & Comment related service

This is the service that will contain video likes and comments related services. As the name suggests through this service we can update the likes and comment for a particular post. It would be the same as other flows that we discussed above.

Recommendation service

Through this service recommend a list of posts based on user preferences. And there are a lot more things going on behind the scene. Let’s see the processes that run behind the scene.

tiktok system design

Then a post is created it’s sent to the Messaging Queue System then the consumer will pick the data and update the data in the Big Data (Hadoop).

There will be a separate server for machine learning services (like PyTorch and Tensorflow) where it will take the data from big data and train the model. This AI model will be used by recommendation services to recommend posts for the given user.

Conclusion

This is a basic level system design of apps like TikTok there could be much other stuff that could be added based on the requirement like tracking user behaviour (which post the user engages with, which post the user gives attention to). Based on that the AI model could be improved to give recommended posts for the user.

--

--