Search code examples
google-cloud-platformgoogle-cloud-datastoreobjectify

Does batch saving entities in Datastore make the entities at the start of the batch available before those at the end?


How exactly does batch saving entities in Datastore work as opposed to saving one at a time?

For example, if you batch save a large amount (let's say 1,000) of entities with:

List<Thing> things = ...
ofy().save().entities(things).now();

Do those entities become available in the datastore all at once? Or one at a time in the order they are in the List?

I want to save a large amount of entities, but I want to give priority to those at the start of the list so that they are available for querying in the Datastore faster than entities at the end of the list. Since saving a batch of entities is much faster than saving one at a time, I am wondering if I am understanding how it works correctly.

Additional question: If you are batch saving 1,000 entities and you encounter an error midway through, will the first 500 entities be saved and the other 500 not? In other words, is there any order to batch saving based on the passed in list? Or is it all over the place?


Solution

  • If you have started a transaction, then everything in the unit of work will either succeed or fail atomically when the transaction is committed.

    If you do not have a transaction running, then yes, you can definitely end up with 500 saved and 500 not. Batch saves are not guaranteed to be atomic.

    I would expect entities at the front of the list to be more likely to be saved when there's a failure, but that's internal Google stuff. I don't know for sure.