How is timeout implemented in resevation or booking system?

Viewed 60

Taking an example of hotel booking system, when a user click on reserve the system holds the room for few minutes, say 20mins, so that the user can fill information and do the payment during that time period. If the user is not able to complete the booking within 20 mins, the system releases the room for new bookings.

How is this timeout implemented and how do we display the timer to the user?

Do we save key-value pair in redis for the reservation with ttl set to 20 mins and then display it to the user in the UI somehow? And when the ttl expires, redis notifies the system which then releases the room? But what if the user is on a third party payments page(stripe/paypal) and redis ttl is reached. If the user makes the payment but we have already released the room by then?

1 Answers

You could store the reservations in one table. Whenever a new reservation is created, an expiration entry is put to a second table or first-in-first-out queue. The expiration entries include the reservation ID. Additionally, they include the expiration time. This could be retrieved from the reservation as well.

The expiration queue is checked once per minute by looking at the eldest entry. All expired reservations are cancelled, unless they carry a confirmed payment. Outdated expiration entries are popped off the queue.

Depending on the volume of reservations, you could simply query the reservation table for reservations which have been created during the last 21..30 minutes without subsequent payment. However, in a no-SQL database, such a query might be too costly to be practical.

The remaining time until expiration can always be determined by looking at the reservation timestamp.

Related