Can REST be stateless in practice?

Consider the situation.

I am writing an application for statistical analysis. The application has several levels.

  • Frontend interface interface for several types of devices, desktop, browser, mobile phone.
  • A mid-level servlet that offers the so-called REST service for these interfaces.
  • A backend that performs extreme calculation of statistical processing.
  • What is associated with an additional database

Because statistical analysis requires tremendous processing power, you never dreamed of delegating such processing to an interface.

  • Statistical analysis consists of procedures or a series of workflow steps.

  • Some steps may require so much processing power; you will not want to repeat them.

  • If you have a workflow of 20 steps, you cannot perform step 20 without first executing step 19, which cannot be completed without the first step of executing 18, and so on and so forth.

  • There are observation points, such that, for example, the statistician should check the results of steps 3, 7, 9, 14, 19 before telling the client side to proceed to the next step.

  • Each of these steps is a so-called REST service request to inform the server supercomputer of the gradual statistical model in memory.

  • There are many work processes. Some workflows may, by the way, share the Results step. for example, Flow [dry]: Step [7] can share Flow [wet]: Step [10]. Due to the processing volume, we absolutely prevent repeating a step that may have already been performed by another thread.

Therefore, you can see that in the so-called REST service, it is not possible that each request is independent of the previous request.

Therefore, how true can the following statement be?

All REST interactions have no status. That is, each request contains all the information necessary for the connector to understand the request, regardless of any requests that may precede it.

Obviously, the application I described requires the request to depend on the previous request. There are three possibilities that I can see regarding this application.

  • My application is not REST compliant, as it cannot match stateless requests. It can use the JAX-RS structure, but using JAX-RS and all the REST attributes does not make it REST simply because it does not meet the criteria without saving.
  • My application is poorly designed. I must ignore the attempt to avoid the temporary and financial cost recovery of the statistical model, even if it took 5-15 minutes for the workflow. Just make sure there is no dependency on previous queries. Repeat the costly steps as necessary.
  • The criteria for obsolescence are becoming obsolete. My understanding of REST is outdated / inferior in that the REST community constantly ignores these criteria.

Is my RESTful application considered?

New Question: ISO 9000

Finally, if my application is not fully reviewed by RESTFul, should all references to "REST" be omitted to pass the ISO 9000 certification?

new edit:

REST IN PIECE

OK, my colleague and I discussed this and decided to name this architecture / pattern REST-in-piece = REST in stages.

+6
source share
2 answers

ISTM, you read too much in statelessness. The REST API supports traditional CRUD operations . The CouchDB API is a good example of how the state of a database is updated by a series of stateless transactions.

Your task is to determine what resources and "state transfer" between them. Each step in your workflow is a different state transition, indicated by a different URI. Each update / change of a resource is accompanied by a POST / PATCH or idempotent PUT or DELETE operation.

If you want to better understand what it means to be RESTful and the reasons underlying each design choice, I recommend spending an hour reading Chapter 5 of Roy Fielding, / a>.

When choosing a design, just think about what the principles of RESTful design are trying to achieve. Set up your project so that requests are safe (do not change state), and that they are executed in ways that can be classified, cached, distributed, etc. Let each step in the workflow transition to a new state with a separate URI so that the user can create backups, branch out in different ways, etc. The whole idea is to create a scalable, flexible design.

+9
source

You are updating the memory model using the REST api. This means that you maintain state on the server between requests.

A REST-complete way to access this should be for the client to maintain state by simply processing the request and returning all the information to build the next request in the response. The server then restores the memory model from the information in the request, and then does its job. Thus, if you are working, for example, in a clustered environment, any of the available servers will be able to process the request.

Regardless of whether or not this is the most efficient way to do this, depends on your application. There are many enterprise applications that use a server-side session and develop load balancing to ensure that clients always use the same nodes in the cluster. Thus, the presence of state on the server side is a completely correct design choice, and there are many ways to implement it. However, server-side state usually complicates scaling, and REST in the purest sense is to avoid server-side state and complexity.

A workaround / trade-off is storing state in some database or storage. Thus, your nodes can retrieve state from disk before processing the request.

It all depends on what you need and what is acceptable for you. As the previous commentator has already noted, do not depend too much on this whole thing. It is clear that someone will have to maintain the state, and the question is simply what is best to put this state for you and how you turn to it. Basically, there are a couple of trade-offs that are mostly related to various โ€œwhatโ€ scenarios. For example, if the server crashes, do you want your client to re-run the entire set of requests to restore the calculation, or do you prefer to simply resend the last request? I can imagine that you really do not need high availability here, and do not mind the low risk that something is not suitable for your customers from time to time. In this case, having server-side state in memory is an acceptable solution.

Assuming your server contains the computation state in some hash map, the REST-ful way to pass the state around then can simply send back the key for the model in response. This is a completely REST-ful API, and you can change the implementation to maintain state or do something else without changing the API when necessary. And the main thing is to be REST-ful: to separate implementation details from the API. Your client does not need to know where you are placing the state or how to store it. All he needs is a representation of the resources of this state that can be manipulated.

Of course, the key must be represented as a URI. I recommend you read Jim Webber's "REST in Practice." This is a great introduction to REST-ful API design.

+2
source

Source: https://habr.com/ru/post/902900/


All Articles