Stateless services (stateless service)

Source: Internet
Author: User
Tags sessionstorage

First, the definition

The stateless Services (stateless service) handles a single request without relying on other requests, that is, all the information required to process a request is either contained in the request or externally to (for example, a database) and the server itself does not store any information

Stateful services (stateful service) instead, it saves some data on its own, and successive requests are associated

Second, the pros and cons

Stateful services are often used to implement transactions (not the only approach, with additional scenarios below). For a common example, buy a product in the mall. You need to go through several steps such as placing your cart, confirming your order, and making payments. Because the HTTP protocol itself is stateless, some additional scenarios are required to implement stateful services. For example, the most common session, the user selected items (shopping cart), saved to the session, when the payment, and then from the shopping cart to take out the product information

Stateful services can be very easy to implement transactions, so it is also valuable. However, it is often said that the server should be designed to be stateless, which is primarily about scalability. If the server is stateless, then for the client, you can send the request to any server, and then you can achieve horizontal scaling by means of load balancing. If the server is stateful, it is not easy to implement because the client needs to always send the request to the same server, the so-called "session Migration" and other scenarios, that is, to solve the problem



Iii. Session and Cookies

Transactions can be implemented based on session and Cookie, and it can be assumed that the session is stateful and the cookie is stateless

Iv. ways to implement transactions without state

It is not necessary to use stateful services to achieve transactions, this article provides several other scenarios for reference
Cite a multiple-commit scenario as an example: users need to submit a lot of data, divided into 2 page submissions



This involves 2 HTTP requests, first committing fields 1, 2, 3, Second commit fields 4, 5, 6

It is easy to implement this requirement with the session, the server only needs to save the first commit data in the session, and then return to the 2nd form as appropriate; then take out the data that was submitted for the first time, and once the data of the second commit is aggregated, the database can be stored together

The same can be done without the session, the server receives the first request, the data as a hidden element, the 2nd table dropdowns returned, so that the user commits the 2nd time, implicitly again submitted the first data; server stores all data in the database
With HTML5, it can be further optimized, the client can save the first commit data in Sessionstorage
Cookies are similar, but they can also be implemented, but not very good.

In general, the 3 alternatives (hidden form elements, sessionstorage, cookies) avoid staging data on the server side, enabling the stateless service. Essentially, all of these 3 scenarios contain all the necessary data, in accordance with the definition at the beginning of this article.

V. Converting a stateful service into a stateless service

According to the definition at the beginning of this article, in addition to putting all the information in the request, there is another way to implement a stateless service, to put information in a separate shareable place, independent of the server exists
For example, the session is also the way to save data on the server to reduce the amount of data transmitted per client request (save traffic), but the session is centralized, such as in a separate session layer. In this case, the server is also stateless and can be scaled horizontally



Vi. Non-state class

By extension, there is a class design in Java that can be called a stateless class. This kind of characteristic is that only the method has no field, in the three Layer architecture (presentation layer, logic layer, persistence layer), the logic layer can often see this kind of
I think the stateless class and the stateless server are the same in mind, and the class itself is stateless, so when the external method calls it, it needs to get all the information needed in the method parameters, not on the class's own State (field value), in the Concurrency environment, Can avoid the side effects of multithreading

Vii. Summary

Stateful services are relatively easy to implement transactions and are a good choice when you don't need to consider horizontal scaling
The advantage of a stateless service is that it is easy to scale horizontally, but there are some additional actions that need to be made when implementing a transaction
A stateful service can be converted to a stateless service by stripping the session and other methods.

On this topic, the following link is also good:
Http://stackoverflow.com/questions/4495950/how-do-stateless-servers-work

Turn http://kyfxbl.iteye.com/blog/1831869

Stateless services (stateless service)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.