How to implement real-time tracking in applications like Swiggy, uber, and Dunzo? When we talk about this question we have these aspects to handle.
- How to send the location
- How to process it and keep the data
- Displaying it
Let’s start by answering these questions one by one.
How to send the location?
Sending location is easy as your mobile devices have GPS and network connectivity. Mobile devices can send their location via an HTTP call. Websockets can also be used to save the extra cost of creating a TCP connection every time which is costly due to a three-way handshake.
How to process it and keep the data
Next is the process of saving it. The problem comes here as you will be getting a lot of requests. Now say you have around 50 thousand riders and the location is sent every 30 seconds to keep it as much real-time as possible. So the total number of requests that you will get is around 100 thousand requests per min which is 1600 per second.
Now when you are getting these number of requests it may be hard to process it on the go or say in a synchronous way, you need to have a buffer where you can keep and process these requests a few seconds later. You can use any queue system for it like rabbitmq, SQS etc. But if you want to make it work for the huge scale you may like to use something like Kafka.
There will be a process running which will take these locations from these queues and process it. Now for processing you can have few workers which take these tasks do it and after getting the result to write it to a place where the last known location of drivers is stored.
Now the application can read the last known location of the driver assigned to anyone. This again can be done using WebSocket to save the connection creation overhead.
Now comes the third question of how to show it. Well, it is not a tough problem as google provides APIs for this purpose.
This is just a basic flow of how you can build it at scale. When you go deeper into each of these aspects you will figure out more problems. Feel free to message me if I can be of any help.
Update: Earlier the service layer between the device driver and message queue was missing in the diagram. Thanks to Abhivendra for figuring it out. Also, keep in mind there are a lot of things abstracted in this so to give you a basic understanding of how it can be done. A lot of components can be added like caching, etc.
If you like the article please share and subscribe. Also, comment if you want me to write about other system designs.
You can install our app for quick updates.