How can i avoid GoogleAppEngine DataStore delay?

2019-05-21 16:52发布

I am new to App Engine and wrote a sample App. If i create or update a entity:

Entity record = new Entity(...);
... set properties
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(record);

and then redirect to a page where the new or updated entity is displayed

resp.sendRedirect("MainPage.jsp");

where the following code is executed

DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();   
Query query = new Query(...).addSort(..., Query.SortDirection.DESCENDING); 
List<Entity> entities = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));

The new record is not in the list. The page is updated (as shown by a timestamp displayed), but the new record or a modification of a existing record is shown only after a delay of up to some seconds when i refresh the page.

How can this be avoided? Is the DataStore possible not suited for such a thing?

I am using the Eclipse local test environment from GAE with Windows XP 64.

2条回答
再贱就再见
2楼-- · 2019-05-21 17:44

Yeah, this is called "eventual consistency" and is the default update model for HRD (now default on all new instances).

So putting an entity and then querying for it might not get is back (evental consistency), but putting it and then getting it (get via a key) will get it back immediately (strong consistency).

Solution is either to:

  1. Use Master-slave datastore, or
  2. If possible, rewrite code to use get instead of queries.

Also, local dev server is only for testing and does not behave exactly as production instances, so you should always (eventually) test on production.

查看更多
叛逆
3楼-- · 2019-05-21 17:46

Since you are running on GAE, your best option is to use its Memcache for short-term retrieval (you still need to put stuff in the DataStore for persistence).

查看更多
登录 后发表回答