Reputation: 547
Theoretical scenario: I have an ecomerce website that keeps an inventory in the GAE Datastore. Each record contains the name of the item and a count. This store has very little traffic and a large inventory so it is unlikely that multiple customers purchase the same item at the same time; this is here so that we do not focus on the datastore limitation about updating the same record several times per second for a sustained period of time.
How do I correctly update the record for an item in the unlikely event that two customers purchase the same item at the same time? It is crucial that we keep the right count for each item.
In the mongo world I would use something like $inc operation. How would you address this scenario for GAE Datastore?
Thanks
Upvotes: 2
Views: 79
Reputation: 42008
Cloud Datastore transactions use optimistic concurrency, so you can attempt 2 transactions at the same time. If the entity changes, one will fail - at which point you simply retry the transaction that failed.
For example, in the Python NDB you can specific number of retry attempts in the transaction decorator.
from google.appengine.ext import ndb
...
@ndb.transactional(retries=5)
def inc_item_count(item_key, count_purchased):
item = item_key.get()
item.count_purchased += count_purchased
item.put()
return True
If you scale beyond where this solution work, you'll want to either explore a Taskqueue based system, or move towards sharding counters the per-item counters.
Upvotes: 2