Overview
Both clustering and distributed evolved from centralized . Distributed and clustered will cooperate with each other, clustered and distributed at the same time. Focus on clusters here
What is a cluster?
Clustering can increase the number of tasks processed per unit time and improve server performance. There
are multiple servers to process tasks, but each task is completed independently by one server.
What is distributed?
Distributed can shorten the processing time of a single task
Like cluster, there are multiple servers to process tasks, but each task is completed by multiple servers , and each server is responsible for completing a small task in a large task
What is centralized?
Centralized is the most traditional kind, all tasks are completed by a large computer
Cluster scenario
Multiple application servers can be clustered on one physical server, and each application server works independently. Then assign a central control server at the front end, which is responsible for distributing the requests sent to this physical server to each application server according to a certain weight.
example
Taking tomcat as an example, you can use the 1*apache+N*tomcat mode
apache as the facade, and use load balancing in the front end to send requests to each tomcat server
java cluster
benefit
Usually a cluster of 32-bit servers replaces a single 64-bit server, which can maximize the performance of the hardware.
1. In a 64-bit program, due to pointer expansion and memory alignment, memory will be wasted.
2. When a single 64-bit server goes down, the snapshot generated is very large, which is difficult to analyze
. 3. A server with large memory means that garbage collection needs to be performed on a larger memory area, which will increase the GC time. , which will prolong the pause of the server
difficulty
Since http requests are stateless, how can the user's state be maintained for session level transactions?
In a single server, the mechanism of session-sessionID is provided to save the user's state
. Now there are multiple servers, how to record the user's state?
There are two general directions:
1. Session stickiness
2. Shared session
session stickiness
This method also becomes an affinity cluster, which creates stickiness for the session, which means that the user accesses the same application server every time.
In this way, it is necessary to record in the front-end server apache, which tomcat the user accesses for the first time, and the back of the user. All requests sent are sent to this
tomcat . The consequence of this is that the load of each server is not balanced , because only when the user accesses for the first time, the load balancing distribution is adopted, but the impact will not be so obvious.
worker.controller.sticky_session=true|false worker.controller.sticky_session_force=true|false
This is the configuration worker.controller.sticky_session for session stickiness in apache . If it
is true, the session sticky mechanism will be turned on. If it is false,
worker.controller.sticky_session_force will be turned off. If it is true, it means that even if the server is down, it will still be sent to this server. If it is false, it will choose to send others to other servers. It is
recommended to choose the former as true and the latter as false. This can not only achieve session stickiness, but also continue to provide services when the server is down
Advantages:
1. Less memory is occupied, because only the sessionID and the corresponding sending server are recorded
Disadvantages:
1. If the server goes down, the session information on this server will be lost
Sync session
This method needs to store all the information of the session in one place, and allows each server node to access these sessions
. There are about three options for this method:
1. cookie synchronization
2. database synchronization
3. in-memory database synchronization
cookie synchronization
Cookie synchronization is to store all session information on the client, and send the session to each request.
Advantages :
1. Does not occupy server memory
2. The server will not lose session information when downtime
Disadvantages:
1. It is placed on the client side, insecure
2. Restricted by the browser, the browser disables cookies
3. Increases traffic and reduces the response time in disguise
4. Session serialization and deserialization require additional time
Database synchronization
Store session information in a database that can be accessed
Advantages :
1. The session information will not be lost when the server is down
Disadvantages:
1. Large memory usage
2. Increased database burden
3. Session serialization and deserialization require additional time
4. Access to the database will require additional time
In-memory database synchronization
Store session information in an accessible in-memory database, such as Redis and memcached
Advantages:
1. Session information will not be lost
when the server is down 2. Fast access speed
Disadvantages:
1. Large memory usage
2. Both session serialization and deserialization require extra time
Summarize
The best session synchronization is the third type. The advantage of in-memory database synchronization
session synchronization is that it is not afraid of a single server downtime, but the resources and speed it occupies are also greater than the session stickiness.
shortcoming
- Node competition for shared resources, such as disk files
- Problems caused by nodes operating on the same resource, such as reading and writing the same file
- 32-bit nodes are limited by maximum memory
- Idleness of resource pools within a node. For example, in connection pooling, it may happen that the connection pool of some nodes is full, and some nodes still have available connections. Consider configuring the same resource pool, centralized JNDI
- The local cache within the node is duplicated because each node has a local cache. Consider using a cache database to store the cache uniformly, such as redis
- Waste of memory, the more nodes, the more classes are loaded repeatedly, the content of the method area in the jvm is similar, but each node has a copy.