What is the recommeneded way to lock an entity (e.g. User(amount_earned)) to perform consistent read --> update --> set operations on Google Cloud Datastore.
Using memcache, we usually do a memcache.add(lock_key), to make sure there is only process/thread modifying a object.
Since memcache's add is atomic and returns false, if key wasn't previously added, it is easy to simulate "locking" using memcache.
How can one simulate similar locking semantic on Google Cloud datastore.
Ideally
with gcp.lock(lock_key)as key:
user.get(user_id) # read
user.update(amount) # update
user.set() # save
# Release the lock, so that other processes can now updated user (identified by user_id) object
You could perhaps use Transactions and Optimistic Locking strategy.
Let's take the example of updating User entity. In order to use optimistic locking, you would have to use a special property (e.g. entity version). The version field will be incremented by one for each update. Steps to update an entity:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With