简体   繁体   中英

How to ensure REST APIs to execute the incoming requests in a sequenced manner

We have a requirement of handling concurrent requests coming from a client to our REST API's which is hosted on the Jetty server , but all the requests should be executed in a sequenced manner that

Does web server guarantee the sequential execution of the requests it receives? If not, what could be the solution?

Assume that we receive the request in sequential order to our APIs.

We Synchronized the Controller method to make sure the threads created per request execute in a sequential manner, but how to guarantee threads come in a sequence to the controller.

Ours is a standalone application and only one Jetty instance is running and no load-balancers, cache implemented.

Jetty will process the requests in whatever order the client asks for them, using the standard behavior defined for HTTP/1.1 and HTTP/2.

If it's a single persistent connection on HTTP/1.1 then the requests are handled in the order they are sent on that HTTP/1.1 connection.

If the client uses multiple HTTP/1.1 connections then each individual connection is handled independently as if they are different unique clients.

If the client uses HTTP/2 then the requests are multiplexed and are handled when the entire request line and headers are received on that HTTP/2 session.

Note that the client in the above statements is whatever HTTP client is talking to Jetty. If you have a load balancer, or firewall, or cache, or TLS/SSL offloader in front of Jetty, then that machine becomes your client. Make sure you properly configure the connection handling on that machine to satisfy your needs as well.

Basically, the various HTTP specs are honored by Jetty, and the behavior you are looking for is 100% controlled by the client, if the client uses a single HTTP/1.1 connection and sends the request pipelined within it then you'll get your desired end result, but if you are javascript on a browser, then you'll get all of the AJAX calls sent in parallel across multiple active browser connections.

Another option, is that you can also artificially force the ordering into your API where a unique identifier provided in a prior response must be given to the next API call in order to progress along the desired ordered calls.

You might want to read past answers to gain more of an insight to your very difficult requirement.

Maybe instead of synchronize the controller method, implement some timed out active waiting on a global sequential semaphore. Something like:

STATIC INT SEMAPHORE := START_SEQUENCE
WHILE (INCOMING_REQUEST.ID != SEMAPHORE) {
  TIMEDOUT_WAIT;
}
PROCESS(INCOMING_REQUEST)
SEMAPHORE := SEMAPHORE+1

This is a simplistic solution, still need to implement some error handling. With this solution, you also need to sequence the requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM