Roman Levin
Roman Levin

Reputation: 471

Are single-entity datastore writes atomic?

In the AppEngine DataStore, given an entity a = MyModel(a=1, b=2), is a.put() atomic? That is, if I run:

a.a=3
a.b=4
a.put()

Am I guaranteed that the entity was updated correctly for all its properties, (assuming a.put() did not raise an exception, that is)?

Dan Sanderson's book says that it is, but I couldn't find concrete documentation on the subject elsewhere.

Upvotes: 1

Views: 203

Answers (2)

Alexander Trakhimenok
Alexander Trakhimenok

Reputation: 6278

Yes & no.

It's atomic but it is eventually consistent. It can make a huge difference to business logic if not taken into account properly.

E.g. if you did:

a = A(id=1)
a.b = 2
a.c = 3
a.put()
.... wait some time
a = A(id=1)
a.b = 4
a.c = 5
a.put()
....
# check now

and if you query a = db.get_by_key(1) it will always have a.b==2 and a.c==3.

But if you query the entity by index it can give different results.

For example if you query at the same time

A.query(A.b==4)
A.query(A.c==5)

then one (any) of the queries can return you 1 record and other return nothing. Or both return 1 record. Or both return nothing for some period of time. Eventually it will always return stable 1 record for each query. But timing is no guaranteed - usually it takes milliseconds but can be few seconds or even minutes or hours in case of big issues.

You may be interested to read more about this: https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/

Upvotes: -2

Amber
Amber

Reputation: 527328

Yes, a single entity's update is atomic.

As noted in this article, a given entity is a single protocol buffer in a Bigtable row. Protocol buffers are always written atomically.

Upvotes: 4

Related Questions