1

I'm working on a Google App Engine project that's written in Java.

Whenever I add an entity into the datastore I use a transaction:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction transaction = datastore.beginTransaction();     
datastore.put(entity);
transaction.commit();

I then count the number of entities that are in the datastore:

Query query = new Query(kind);
query.setFilter(filter);

PreparedQuery preparedQuery = datastore.prepare(query); 
int count = preparedQuery.countEntities(FetchOptions.Builder.withDefaults());

System.out.println("count = " + count);

I've noticed that sometimes the datastore can be inconsistent. Like sometimes I'll add an entity, but the count doesn't show up. If I then add another entity the count goes up by 2.

Why does this happen? How do I stop it from happening?

1 Answer 1

4

You are experiencing the effects of "Eventual Consistency", which is a side effect of using the High Replication Datastore. Basically, you are calling the query before the data has time to replicate across data centres. You can avoid this, by using "Ancestor Queries", which always results in "Strongly Consistent" queries. Suggest you read this, which explains it in detail.

Sign up to request clarification or add additional context in comments.

2 Comments

I think my problem was that I was creating entities without a parent entity. I'm new to the Google App Engine, though.
kind of. by specifying a parent entity, you are forcing the entities to persist within the same "entity group". if you then perform an ancestor query against those entities, you will get "strongly consistent" results, which will solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.