I am a beginner in programming, and I have recently came across this topic and having a lot of questions in my mind about this topic. Assume that I am creating this Booking application in PHP as Backend with SQL as the database and HTML + JS as the front-end, and for understanding purposes this is an empty booking system.
I have 20 seats to be booked and launching the server at a particular time and more than 100 users are trying to book these tickets after booking, a dummy payment process for little realistic. Assuming two users selecting the same seat or ticket at the same time what happens then? how the program will choose between them?
Assuming One user wants to spam others so he just selects a seat and wait for long in the payment process, what happens to that seat?
and so many questions like this? wondering how the real time applications like airline ticket system or amazon purchasing with limited stocks works?
Thanks in advance.
Different systems use different approaches/patterns. Most common are:
- “Lock” the scarce resource at some point so nobody else can take it, with the lock expiring after a certain time period if you don’t complete the operation. For example some ticket booking systems will show you that you’ve got 10 minutes to finish the payment process. At the end of 10mins the ticket(s) go back to being available for someone else to book. This is how tickets via travel agents work (or used to work); they’d be able to put a hold on the tickets for 24 or 48 hours (for example, I imagine the systems had specific allowed times), within which time they could complete the booking at that price.
- the resource (ticket etc) isn’t reserved/locked until paid for. So you can have an item in your shopping cart right up to the point you pay for it but if someone else buys it before you then when you try to pay the system will say ‘oops, no longer available’.
Which approach you’d choose depends on your requirements. How likely is it there’ll be conflicts? How much do you want to annoy your customers vs how likely will they try to game the system and keep items in cart without buying?
Thank you so much for your time taking reply, I really appreciate it.
Lock is the same concept I’ve been thinking about, even I’ve tested it by opening a random movie ticket booking website and I’ve selected a two seats for a particular movie with some time and it didn’t made any payment process just selected the seats, meanwhile I’ve took another account and tried to select the same seats for that movie with same timing but it was occupied so I came to a conclusion that some time lock system is implemented for payment process for the seats. But how to create one is there any blogs, tutorial videos that teach about them. Any how thanks for the reply. If you find any related blogs, tutorials please let me know.
Typically “pessimistic locking” with some time bound involves some kind of background task running on the backend that checks the pending reservation table, and when the reservation window expires, removes the reservation lock. You also store this expiration on the web page somewhere so the user can be notified when it expires.
The details of this approach are astonishingly tricky, and very hard to get right without race conditions and/or deadlock. Even airline reservations get this wrong sometimes and double-book seats. If you can find a tutorial or example of reservation systems, you might want to go with that as a starting point. I found quite a few resources on github, though I can’t vouch for the correctness of any of them.
“Optimistic Locking” (checking for collisions only on completion) is a fair bit simpler: you just add versions to the resource, and if the current version is higher or equal to the expected version, you bail. You don’t usually want to do this sort of thing for a booking system however, since it’s lousy UX for someone to go through the whole process only to find someone sniped their reservation while they were booking it. Still, it is an option for very low-traffic systems, especially if the booking process is fast.
Thank you for the detailed explanation, I didn’t know that booking systems are so much complicated and tricky to implement, and I almost forgot about github, I will search more about this topic and let everyone know about that here.
I want to know if any user locks any seat and time for unlcok seat is 10 mins.
User starts transaction at 9 min 30 secs and complete at 10 min5 secs. Then what should be the behavior. Ticket should be booked or cancel?
websites that I usually use, after the time limit is up, whatever you do, they say “session is expired” and you must start again
Suppose, I am booking a ticket from amazon. I locked a ticket for 10 mins and my transaction finishes in 10min 5 secs. I am still login. So how system should behave? Ticket should be booked or cancel. Amount which was deducted from my account will successful or revert.