Building a Scalable Train Booking System
Source Code : https://github.com/AnkursainiRD/-v2_Train_Booking_Management
Day 1
Hey guys. I am starting to build a Full Stack Scalable Train Booking System. Which can handle concurrency at on its peak time. And i will add many features in future.
The Tech stack i gonna use in this will be Node.js, Redis, Docker, Kubernetes, AWS, MongoDB for Backend. And React, Redux for Frontend.
So first i created all the basic template code for server. And also created some data models for my project like - Train Model, Booking, User, Routes, Schedule, Payments Notification Model.
And This is my folder structure for whole project. Obviously it will expands when it required.
Day 2
Initially i am so confused because when i creating the model of train a question hit in my mind. How can i store the routes of that Train not just stations details also a better searching train technique between Two stations by User.
Then i visit the Google and AI to took a reference and solution for this. And i end up with this solution.
— Instead of storing just station name and code, I can store stations for routes with their location coordinates. And MongoDB is also provide a feature called Geo JSON.
— Also a additional feature called 2D Sphere Indexing. Which help me to query the large amount of data in less time. So with these coordinates i can perform various complex query and advanced features.
Features like Distance Calculator, Nearby Stations, Map Animation, Route Marking in Map and many more.
Day 3
Now the main problem is that what happen if more than of 2 user simultaneously tried to book a same seat. How this will handled. So then i learned the concept of Concurrency Handling. This with some technique we can achieve the solution for concurrency. Then i look out for ideas which is commonly used in market to tackle this type of situation.
— First thing which can help is Transaction technique of database.
— Second solution is to implement Redis Lock System on that transaction.
So after combining of them we get the system which can handle the 200k Request/minute on server. Once a user triggered a Booking API that lock is has been set for with that seat for that user. And lock will release either user complete the Transaction or failed the Transaction.
And while a lock is set then any other user can’t book that same seat. The source code of this project is on my GitHub.
Day 4
So most of the things in completed. Then i realized the OTP verification is make the Authentication more secured and scalable. So i just start writing the API for OTP verification. And then one more thing hit in my mind that why would i store user’s OTP in my database but if i am not did this so how can i verify the right OTP. So i decided to use Redis.
— Store user’s email as Key and its generated OTP as Value with expiry time.
— If user hit multiple Sign Up API at short amount of time, then every time the value of that email rewrite by Redis.
So it help to avoid unnecessary DB write call and also increase the response time because OTP stored in Redis Cache. And its cleared after given time.
Day 5
I was trying that when a user search trains between boarding station and destination station so it along with train the distance and travel time also show with that. As i share that i stored station coordinates. So i am trying to calculate the distance with Redis geoAdd and geoDist functions but they will return a inaccurate answer so then i try to find other solution and get the geoLib package that return accurate distance and with that i learned that sometime you don’t need to use big tech. You can perform some task with other simple library those was created for these kind of tasks. And there’s nothing odd to use these small libraries.
Subscribe to my newsletter
Read articles from Ankur saini directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ankur saini
Ankur saini
Backend developer with a strong foundation in Node.js, Kafka, Redis, Docker, Kubernetes, AWS, and Django. Passionate about designing scalable, event-driven systems and optimizing backend performance for efficient cloud deployment