gsinha
gsinha

Reputation: 1185

Advice needed for Concurrent read and write scenario in GAE Python NDB

I am developing a ticket booking website using GAE Python. This website lists available vehicles on the selected date and allows the customer to book it.
When the customer selects a vehicle and proceeds to booking, I want to temporarily block the vehicle for that day so that nobody else books it using our website. This temporary block should get revoked automatically in next two minutes if the customer fails to make the payment correctly.
I am planning to have a model like below:

class Availability(ndb.Model): 
    vehicle = ndb.StringProperty() 
    date = ndb.DateTimeProperty() 
    status = ndb.StringProperty() #"available" or "booked" 
    temporary_blocking_time = ndb.DateTimeProperty() 

I would update the "temporary_blocking_time" variable for the selected vehicle and date. When any other customer searches for a vehicle on this same date, the "temporary_blocking_time" for that vehicle would be checked and if it is less than two minutes old, that vehicle would not be listed for the customer to book.

t0 < t1 < t2 < t3
The problem with this logic: When a customer "X" wants to proceed to book a given vehicle, I read the "temporary_blocking_time" of that vehicle(at time "t1") and write the current time to that(at time "t2").
Problem can arise when another customer has also selected the same vehicle same date, and has read the the "temporary_blocking_time" at time "t0" and write it back at "t3". This would overwrite customer "X"s booking.

To add to this problem, consistency issues might also come.

How can I improve the logic design to solve this problem

Upvotes: 3

Views: 253

Answers (2)

Patrick Costello
Patrick Costello

Reputation: 3626

Consider doing the get to see if the temporary_blocking_time is active and the put to set it inside a transaction. This would make the put at t3 fail since the put at t2 happened after t0.

Upvotes: 1

Tunguska
Tunguska

Reputation: 21

Have you considered Computed Properties?

https://developers.google.com/appengine/docs/python/ndb/properties#computed

Upvotes: 0

Related Questions