7.7 Service Statelessness
Each invocation of a service operation is completely independent from any other invocation, whether by the same service consumer or any other service consumer. The Service Statelessness principle offers various benefits centered around improved scalability by which additional stateless service instances can be easily provisioned on available environments. With the advent of cloud computing, on-demand scaling out of services is considered as a natural evolutionary step for stateless services.
Many real-life business scenarios can be expressed as business processes that include automated steps, which can require manual intervention. Designing and implementing such a process requires some state to be maintained for the duration of the process. Executing an instance of the process definition forms the notion of a session or transaction across multiple service invocations. Therefore, a service implementing the execution of a business process cannot be stateless and may need to even maintain context information over extended periods.
An orchestration infrastructure, such as a WS-BPEL execution environment, will support state maintenance either by storing state in the local stack of the thread executing the process or in a permanent relational data store. A permanent relational data store ensures that a process instance can be continued after a system crash or other interruption. This style of statefulness is built into WS-BPEL and its execution environment, so there is little to be aware of when designing such a service. Designing a service that aggregates other service invocations in its implementation without utilizing WS-BPEL will require the developer to decide whether to store temporary data in a relational data store for later recovery in case of failure.
Another aspect of achieving service statelessness occurs when a service must establish some form of session with a service consumer, such as when the state is not kept in the calling logic but in the called service itself. Compare this to the shopping cart scenario in which a service consumer uses a service repeatedly to collect a set of data, which is then committed all in one final step. Java Servlets, which at the core also offer a stateless programming model, leverage the HTTPSession information and the concept of cookies to enable data to be stored on behalf of a certain client.
For REST services, using cookies as handles to store client state violates the stateless constraint of a REST-style architecture. Any maintenance of server-side session state is not recommended, and REST limits application state to be held on the client and not on the server.
Using cookies breaks the statelessness model as the server is expected to maintain a reference to a session and use the information in the incoming cookie for discovery. Each invocation is expected to carry the complete contextual information without any references to any previous interaction, promoting scalability where any available service instance can process this request. For idempotent requests, the failed request can be redirected to another functioning stateless service. Note that the convenience of server-side session maintenance and the associated personalization benefits are sacrificed for superior scalability and optimum resource usage.
REST services can read or write state information for various domain entities from or to databases or other persistent stores, but storing client state violates the statelessness constraint with implications for scalability and resilience. SOAP-based Web services have no such constraints and the servlet model is leveraged by JAX-WS to handle stateful interactions.
Two methods are available for clients to store state as part of a repeated interaction with a servlet. The first allows the data to be sent back and forth with each request, and the amount of data grows with each invocation. The second method enables the servlet to maintain the state, and the servlet sends back a key to this data (the cookie). The client sends the same key along for a subsequent request, which allows the servlet to retrieve the appropriate state for this particular client. The interface used to store the data is called javax.servlet.http.HTTPSession.
The behavior described in maintaining a key is a well-defined part of the standard servlet programming model, and APIs are available to enable its use. The javax.servlet.http.HTTPSession interface offers methods for storing simple key-value pairs. JAX-WS describes a mechanism to pass a reference of the HTTPSession instance into the service implementation class.
A Web service implementation can be made stateful by leveraging HTTP sessions and the JAX-WS support for the sessions. This requires that the service consumer receives and reuses the cookie sent back with the response to the original HTTP POST request.
HTTP sessions and JAX-WS are only applicable for services with SOAP/HTTP bindings, although similar behavior in a service not accessed over HTTP can be created using the same principles. At invocation by a specific service consumer, the service can return a unique identifier to the service consumer, such as in the SOAP response header, and expect that identifier to be returned with every subsequent request. The service uses the identifier as a key into a table where the data is stored.
How the data is physically stored depends on the developer’s requirements. The data stored on behalf of specific service consumers must be recoverable across a server restart persistently in a relational database, which can be accessed using JDBC or a similar mechanism.
Note that state as discussed in this section is usually transient and only exists for the duration of the interaction with the relevant service consumer. Business-relevant data, which needs to be persisted permanently, would not be stored using the mechanisms described. Business data should be stored using data access APIs, such as JDBC, or persistence frameworks, such as JPA or Hibernate.