Does Google Cloud Datastore support atomic operations, for counters and such?
google-cloud-datastore
Solution
Not directly, but you can simulate an atomic increment operation using transactions. In the simplest case, you use a single entity and increment with a transactional read+write. Such a transaction will fail if there is a concurrent increment, so the counter does not scale well. Instead, you can operate on n entities with keys in the form of `[('Counter', 'MyCounterX'), ('Elt', 'k')]` for some number k, and property 'Count':
To increment, pick a random number between 1 and n, and attempt a transactional read+write. If the key doesn't exist, write a new entity on the given key with count=1. If the transaction fails, you can retry with a new random number. You could also include logic for the application to increase n on the fly it it starts hitting contention frequently.
To retrieve the count, do an ancestor query using root [('Counter', 'MyCounterX')] with a projection on the Count property, and then add up all the counts.
You can see code which implements this in the second block here.
Problem
Does Google Cloud Datastore support atomic operations, for counters and such? I don't see anything about in the documentation and I noticed that updates are basically getting the whole object, updating a field, then sending the whole object back which seems like it would make atomic operations impossible. https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Updating_an_entity