Google App Engine Datastore, how to deal with data inconsistency?

I am porting a simple CRUD rest service and I realized that the data-store consistency is not immediate.

Currently I make a POST request to create the entity with and get the status code 202 (because I know that the entity is not persisted yet). Then a GET request to get the created entity, if the status is 404 I sleep during two seconds, and I keep requesting until I get a 200.

Is this an acceptable way to deal with data-store eventual consistency? Should I make the same read trys until I got the entity in the server side? Is there an alternative?

Upvotes: 0

Views: 93

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881497

Your server-side Java code which creates the entity in response to the POST can then get that entity's Key by its method .getKey(). Then, it uses KeyFactory.keyToString (see https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/KeyFactory#keyToString(com.google.appengine.api.datastore.Key) ) on that Key to obtain a "websafe string" form of the key which it sends to your client as part of the response.

The client-side GET can then include that "websafe string" and the server can make the Key object again using KeyFactory.stringToKey.

Last but not least, a datastore.get(key) on an instance of DataStoreService, see https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/DatastoreService , returns the corresponding entity with strong consistency -- a fact that's unfortunately not clearly documented for Java but holds just as well as for Python, for which it is clearly documented at https://cloud.google.com/developers/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/#h.tf76fya5nqk8 ...

So, by properly dancing the key-based minuet, you should be fine without needing to retry-and-wait on 404s!-)

Upvotes: 2

Related Questions