Concurrency Session Handling

Ankur sainiAnkur saini
2 min read

In my Train Booking project i have implemented the Redis Lock which can provide a isolation on a particular seat for a particular user. Everything was going in right direction but after some time i faced a problem where user clicked the button on booking and triggered the Redis lock on that seat.

But what happen when a user exit the process of don’t complete the payment. Initially when user clicked on book button the partially booking will created means the status of ticket is pending as well as payment status is also pending. And convert into success status once Payment is completed.

But if user backout the process before the final payment but creation of partial booking is already made, although ticket is not confirmed but the partial booking document for that seat number and train id will remain into the Database.

Then i start finding some solutions for this situation and find out that if i create a lock for a time frame and once time frame is expired then the lock will release automatically and delete that partial booking that created in Database.

But how the function will know that time is expired lock is released and had to delete that partial booking document. I have 2 options to handle this situation.

  1. Create a function which run every 2 minutes or dedicated time frame and clear the booking document. I mean using set Interval or something. But it will reduce the performance drastically and make server slow also consume resources. Cause whether a user is booking seat or not this function will run every given time frame.

  2. Instead of this i can follow a Pub-Sub model. There a special event in Redis called [ __keyevent@0__:expired ]. So my server will subscribe this on starting of server. Then there is a listenForExpiry function listens to Redis events, specifically when a key expires.
    — And as soon as a key expired an event is triggered, the listenForExpiry function listens for it using the pmessage handler.

    — Then this listenForExpiry function called a cleaner function which performs data deletion task in Database and update the documents.

You can checkout the source code in my GitHub repository.
Code:https://github.com/AnkursainiRD/-v2_Train_Booking_Management

0
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