“Data consistency” and “high availability” are essentially ways to improve the whole by increasing complexity. This article focuses on something that makes systems simpler and easier to maintain – “scalability”, with stateless being the first subject.

1. Stateless and stateful services

1.1 Stateless Services

Each request from the client must have self-describing information that identifies the client. The server does not save any client requester information.

The benefits of statelessness

  • Client requests do not depend on server information, and any multiple requests do not have to access the same service
  • Server cluster and state transparent to clients = Server can migrate and scale freely = reduce server storage pressure

1.2 Stateful services

Stateful service, that is, the server needs to record the client information of each session, so as to identify the client identity, according to the user identity of the request processing, a typical design such as session in Tomcat.

For example, login: After the user logs in, we save the information of the user in the server session, and give the user a cookie value to record the corresponding session. And then the next time we request, the user comes in with a cookie value, we’ll be able to identify the corresponding session, and we’ll find the user’s information.

Stateful defects

  • The server saves a large amount of data, which increases the pressure on the server
  • The server saves the user state and cannot scale out horizontally
  • Client requests depend on the server, and multiple requests must access the same server

A stateful determination is whether two requests from the same originator have a context relationship on the server side.

  • In the case of stateful requests, the server side typically holds information about the request, and each request can use the previous request information by default.
  • For stateless requests, the processing information on the server side must all come from the information carried by the request and the common information available to all requests.

Stateless versus stateless

Stateless server programs, the most famous of which are WEB servers.

State of the server has a wider range of applications, such as MSN, network games and other servers. It maintains state information for each connection on the server side, and when the server receives the sent request for each connection, it can reproduce the context relationship from locally stored information.

Pure functional programming is stateless. Have a state, also called side effects.

Stateless services scale: It’s easy to scale horizontally by adding servers to the back end and load balancing the front end. Scaling becomes complicated when there are a large number of “stateful” business processes in the system.

2. Stateful and stateless objects

2.1 What are stateful and stateless

  • Stateful means having data storage capabilities. Stateful beans, which were objects that had instance variables and could store data, were non-thread-safe. No state is retained between method invocations.

  • Stateless is a single operation that does not save data. Stateless beans are objects that have no instance variables. Cannot save data, is immutable class, is thread safe.

Deeper understanding through code

2.2 Stateful Beans

/ * * * @ contract: the public number: Java half candy * @ desc: stateful bean, with the state, such as user attributes, and the user save 偖 function, is variable. * @link: */ @Getter @Setter public class StatefulBean { public int state; // Public TestParam param is not thread safe because the user is a reference object in a multithreaded environment; }Copy the code

Stateless Beans

/** * @desc: stateless bean, cannot store 偖 data. It's immutable because it doesn't have any properties. There is only one system method to operate * @link: */ public class StatelessBeanService {// The borderService does not have status information, although the orderService property exists. Stateless Bean. OrderService OrderService; public List<TestParam> findUser(String Id) { return null; }}Copy the code

3. Stateless and stateful in Spring

Through the above analysis, I believe you have a certain understanding of stateful and stateless.

  • Stateless beans are suitable for immutable patterns, and the technique is the singleton pattern, which can share instances and improve performance.
  • Stateful beans are unsafe in multithreaded environments, so use the Prototype pattern. Prototype: A new bean instance is created every time a bean is requested.

By default, the instance obtained from the Spring Bean factory is Singleton (the scope property is Singleton), and only one shared bean instance exists in the container.

Once you understand the relationship between the two, the principle of scope selection is easy: The Prototype scope should be used for stateful beans, while the Singleton scope should be used for stateless beans.

For example, the Service layer and Dao layer use the default Singleton. Although the Service class also has attributes such as Dao, these Dao classes are stateless, which is equivalent to immutable classes, so it does not affect the Dao class.

The default Struts2 implementation is Prototype, since actions are statically dependent on User and BizEntity instances. This is not safe in multithreaded environments. In Spring, Struts2 Action, the scope is set to the prototype scope.

4. Stateless and stateful protocols

Stateless protocol: The next link does not remember the information from this link.

  • HTTP and UDP are stateless protocols
  • TCP and FTP are stateful protocols

4.1 Http Stateless

The standard HTTP protocol is stateless and connectionless

  1. Standard HTTP protocol refers to HTTP protocol that does not include cookies, sessions, applications, etc. None of these are standard protocols, although various web application providers, implementation languages, Web containers, etc., all support it by default
  2. What does connectionless mean
    • Each access is connectionless, and the server processes the access queues one by one, closes the connection once it’s done, and then processes the next one
    • Connectionless means to limit processing to one request per connection. When the server finishes processing the customer’s request and receives the customer’s reply, it disconnects

I see a lot of blurring (official or tutorial) about statelessness through a layer of frosted glass.

  1. Protocol has no memory for transaction processing [transaction processing] [memory]
  2. There is no context for the same URL request
  3. Each request is independent, and its performance and results are not directly related to the previous request and subsequent requests. It is not directly affected by the response of the previous request, nor by the response of the subsequent request.
  4. The client state is not saved in the server. The client must request the server each time with its own state.

Related articles

  1. What exactly does “state” mean in HTTP protocol stateless