开发者

Write to GAE datastore asynchronously

开发者 https://www.devze.com 2023-04-06 17:09 出处:网络
In my Java app, sometimes my users do some work that requires a datastore write, but I don\'t want to keep the user waiting while the datastore is writing. I want to immediately return a response to t

In my Java app, sometimes my users do some work that requires a datastore write, but I don't want to keep the user waiting while the datastore is writing. I want to immediately return a response to the user while the data is stored in the background.

It s开发者_Python百科eems fairly clear that I could do this by using GAE task queues, enqueueing a task to store the data. But I also see that there's an Async datastore API, which seems like it would be much easier than dealing with task queues.

Can I just call AsyncDatastoreService.put() and then return from my servlet? Will that API store my data without keeping my users waiting?


I think you are right that the Async calls seem easier. However, the docs for AsyncDatastore mention one caveat that you should consider:

Note: Exceptions are not thrown until you call the get() method. Calling this method allows you to verify that the asynchronous operation succeeded.

The "get" in that note is being called on the Future object returned by the async call. If you just return from your servlet without ever calling get on the Future object, you might not know for sure whether your put() worked.

With a queued task, you can handle the error cases more explicitly, or just rely on the automatic retries. If all you want to queue is datastore puts, you should be able to create (or find) a utility class that does most of the work for you.


Unfortunately, there aren't any really good solutions here. You can enqueue a task, but there's several big problems with that:

  • Task payloads are limited in size, and that size is smaller than the entity size limit.
  • Writing a record to the datastore is actually pretty fast, in wall-clock time. A significant part of the cost, too, is serializing the data, which you have to do to add it to the task queue anyway.
  • By using the task queue, you're creating more eventual consistency - the user may come back and not see their changes applied, because the task has not yet executed. You may also be introducing transaction issues - how do you handle concurrent updates?
  • If something fails, it could take an arbitrarily long time to apply the user's updates. In such situations, it probably would have been better to simply return an error to the user.

My recommendation would be to use the async API where possible, but to always write to the datastore directly. Note that you need to wait on all your outstanding API calls, as Peter points out, or you won't know if they failed - and if you don't wait on them, the app server will, before returning a response to the user.


If all you need is for the user to have a responsive interface while stuff churns in the back on the db, all you have to do is make an asynchronous call at the client level, aka do some ajax that sends the db write request, changes imemdiatelly the users display, and then upon an ajax request callback update the view with whatever is it you wish.

You can easily add GWT support to you GAE project (either via eclipse plugin or maven gae plugin) and have the time of your life doing asynchronous stuff.

0

精彩评论

暂无评论...
验证码 换一张
取 消