Does Google Cloud Datastore store atomic operations, for counters, etc.?

Does Google Cloud Datastore support atomic operations, for counters, etc.? I don't see anything in the documentation, and I noticed that updates basically get the whole object, update the field, and then send the whole object back, which seems to make it impossible for atomic operations to be performed. https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Updating_an_entity

+3


source to share


2 answers


Directly, but you can simulate the create atom operation using transactions . In the simplest case, you use a single object and grow it using a transactional read + write. Such a transaction will fail if there is a concurrent increase, so the counter does not scale well. Instead, you can work with n objects with keys in the form [('Counter', 'MyCounterX'), ('Elt', 'k')]

for some number k, and the "Count" property is:

To increase, pick a random number between 1 and n and try to perform a transactional read + write. If the key does not exist, write a new object 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, which often starts to run into conflict.



To get the counter, run an ancestor query using root [('Counter', 'MyCounterX')] projected onto the Count property, and then add all the counts.

You can see the code that implements this in the second block here .

+5


source


Push your updates into an ordered queue (like Google Pub / Sub) and use a single processor / service, which is the only service that has write permissions. The advantage of this method is that you can choose a persistent platform based on the data and queries available rather than atomic / acid requirements.



A simpler (and cheaper) option is to use a background function that is called by Pub / Sub, so the assigned serverless function gets messages from Pub / Sub and you just concentrate on writing the increment logic.

0


source







All Articles