Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

How to handle Asynchronous operations in REST

How to handle Asynchronous operations in REST


I need to understand what are the approaches with pros-n-cons to handle asynchronous operations in REST. Some approaches I found:

1- Resource Based: Where status of operation is modeled as status. User makes an asyn REST call (PUT, POST etc) gets Accepted or In-Progress response (202). Further status uri is polled repeatedly via GET to check status/progress/messages from operation execution.

Question: How long this resource be active at Server? If client polls in large intervals where in between operation completes, how do we return status? Seems like persisting execution status would work. But how long to persist, when to archive/delete, is this kind of standard approach?

2- Callback Based: Where Async request required to have a callbackURI, request gets processed asynchronously and upon completion makes a call to callbackURI with operation status/result.

Question: This seems more elegant, less overhead at server-side. But scenarios where callback server is intermittently down not responding etc, how do we handle this? Implement a typical retries where callbackURI provides retries configuration as well? Is there any other downside to this approach?

3- Servlet 3.0 Asynchronous support: Where a HTTP client to Java Servlet makes a connections, which remains open until it is explicitly closed, until closed client-server can communicate asynchronously over it.

Question: Since its Servlet 3.0 spec, I think Jersey, Spring REST implementation doesn't utilizes this approach as of now. Is there any specific REST implementation which utilizes similar approach or pointer on ways to make it possible?

4- Any other approaches may be commercial ones?

Answer by user2256686 for How to handle Asynchronous operations in REST


I think, the approach depends on time gap between initial request and the end of operation.

  • For short-time operations ( < 10s ) I would just keep the request open and return response when operation finished;
  • For long operations ( < 30m ) I would use servlet 3.0 or Comet model;
  • For extremely long operations ( hours, days ) good enough way, as for me, is just client-based polling or Comet with big timeouts.

Answer by javivelasco for How to handle Asynchronous operations in REST


I'm dealing now with the same situation and found the common approach of using Location header response to give a resource that can be monitored to check status (by polling of course). That seems to be the best, but in my case, I'm not creating a resource so I don't have a location to check the status (my async process is just to build a cache page).

You can always use your own headers to give an estimated time to complete the operation. Anyway I'm thinking of using Retry-After header to give an estimated time. What do you guys think?

Answer by matsev for How to handle Asynchronous operations in REST


Spring 3.2+ supports the async features of Servlet 3.0. From the Spring Blog:

you can make any existing controller method asynchronous by changing it to return a Callable. For example a controller method that returns a view name, can return Callable instead. An @ResponseBody that returns an object called Person can return Callable instead. And the same is true for any other controller return value type.

Jersey 2+ also supports asyncronous servers. See the Asynchronous Services and Clients chapter in the reference docs.

Answer by robert_difalco for How to handle Asynchronous operations in REST


I know this is old but I thought I'd chime in here to say that if what you want is something that can scale out in a stateless environment then you should go with your first option. You can perform the underlying operation anywhere and if you put the result in something like redis it will not matter to what web server the client makes subsequent polling requests. I'll usually put the polling interval in the response I sent to the client. When there a result is ready I will return the client a SEE OTHER that includes the id of the result in the URI.

Answer by Hemus7 for How to handle Asynchronous operations in REST


Jersey 2.X unfortunately does not add in any performance improvement in reducing the response time or increasing the throughput by using the asynchronous way. http://www.jayway.com/2014/05/16/async-servlets/ details the root cause. I have tested the asynchronous with my service and overall it has given a negative result :(


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.