Home Operation and Maintenance Linux Operation and Maintenance Introduction to session processing methods in Linux cluster/distributed environment

Introduction to session processing methods in Linux cluster/distributed environment

Sep 08, 2017 am 09:55 AM
linux session distributed

This article mainly introduces you to the five strategies for session processing in Linux cluster/distributed environment. The article introduces it in great detail through sample codes and pictures. It has certain reference learning value for everyone's study or work. Friends who need it, please follow the editor to learn together.

Preface

Generally, after we build a cluster environment, one issue we have to consider is how to handle sessions generated by user access. If no processing is done, users will log in frequently. For example, there are two servers A and B in the cluster. When the user visits the website for the first time, Nginx will forward the user request to server A through its load balancing mechanism. This will At this time, server A will create a Session for the user. When the user sends a request for the second time, Nginx will load balance the request to server B. At this time, server B does not have a Session, so the user will be kicked to the login page. This will greatly reduce the user experience and lead to the loss of users. This situation should never occur in the project.

We should process the generated Session to ensure user experience through sticky Session, Session copying or Session sharing.

Below I will explain 5 Session processing strategies and analyze their advantages and disadvantages. Not much to say, let’s take a look at the detailed introduction.

First type: sticky session

Principle: Sticky Session refers to locking the user to a certain server , such as the example mentioned above, when the user requests for the first time, the load balancer forwards the user's request to server A. If the load balancer sets a sticky session, then every subsequent request of the user will be forwarded to server A. , which is equivalent to sticking the user and server A together. This is the sticky Session mechanism.

Advantages: Simple, no need to do any processing on the session.

Disadvantages: Lack of fault tolerance. If the currently accessed server fails and the user is transferred to the second server, his session information will be invalid.

Applicable scenarios: Failure has a small impact on customers; server failure is a low-probability event.

Implementation method: Take Nginx as an example. Sticky Session can be achieved by configuring the ip_hash attribute in the upstream module.


upstream mycluster{
 #这里添加的是上面启动好的两台Tomcat服务器
 ip_hash;#粘性Session
  server 192.168.22.229:8080 weight=1;
  server 192.168.22.230:8080 weight=1;
}
Copy after login

Second type: server session replication

Principle: Any When the session on a server changes (addition, deletion, modification), the node will serialize all the contents of the session and then broadcast it to all other nodes, regardless of whether other servers need the session, to ensure session synchronization.

Advantages: Fault-tolerant, sessions between servers can respond in real time.

Disadvantages: It will put a certain pressure on the network load. If the number of sessions is large, it may cause network congestion and slow down server performance.

Implementation method:

① Set tomcat, server.xml turns on tomcat cluster function

Introduction to session processing methods in Linux cluster/distributed environment

Address: fill in Just use the local IP address and set the port number to prevent port conflicts.

② Add information to the application: notify that the application is currently in a cluster environment and supports distributed

Add options in web.xml<distributable/>

The third type: session sharing mechanism

Use distributed caching solutions such as memcached and redis, but Memcached or Redis must be cluster.

There are two mechanisms for using Session sharing. The two situations are as follows:

① Sticky session processing method

Principle: Different tomcat designated access Different master memcached. Information between multiple Memcached is synchronized, enabling master-slave backup and high availability. When a user accesses, he first creates a session in tomcat, and then copies the session to its corresponding memcahed. Memcache only plays a backup role, and all reading and writing are done on tomcat. When a certain tomcat hangs up, the cluster locates the user's access to the backup tomcat, and then searches for the session based on the SessionId stored in the cookie. If it cannot find the session, it goes to the corresponding memcached to retrieve the session. After finding it, it copies it to the backup tomcat. superior.

Introduction to session processing methods in Linux cluster/distributed environment

② Non-sticky session processing method

Principle: memcached does master-slave replication, and writes to sessions are written to the slave memcached service , reads are all read from the main memcached, tomcat itself does not store the session

Introduction to session processing methods in Linux cluster/distributed environment

Advantages:Fault-tolerant, session responds in real time.

Implementation method: Use the open source msm plug-in to solve the session sharing between tomcats: Memcached_Session_Manager (MSM)

a. 复制相关jar包到tomcat/lib 目录下

JAVA memcached客户端:spymemcached.jarmsm项目相关的jar包:1. 核心包,memcached-session-manager-{version}.jar2. Tomcat版本对应的jar包:memcached-session-manager-tc{tomcat-version}-{version}.jar序列化工具包:可选kryo,javolution,xstream等,不设置时使用jdk默认序列化。
Copy after login

b. 配置Context.xml ,加入处理Session的Manager

粘性模式配置:

Introduction to session processing methods in Linux cluster/distributed environment

非粘性配置:

Introduction to session processing methods in Linux cluster/distributed environment 

第四种:session持久化到数据库

原理:就不用多说了吧,拿出一个数据库,专门用来存储session信息。保证session的持久化。

优点:服务器出现问题,session不会丢失

缺点:如果网站的访问量很大,把session存储到数据库中,会对数据库造成很大压力,还需要增加额外的开销维护数据库。

第五种terracotta实现session复制

原理:Terracotta的基本原理是对于集群间共享的数据,当在一个节点发生变化的时候,Terracotta只把变化的部分发送给Terracotta服务器,然后由服务器把它转发给真正需要这个数据的节点。可以看成是对第二种方案的优化。

Introduction to session processing methods in Linux cluster/distributed environment

优点:这样对网络的压力就非常小,各个节点也不必浪费CPU时间和内存进行大量的序列化操作。把这种集群间数据共享的机制应用在session同步上,既避免了对数据库的依赖,又能达到负载均衡和灾难恢复的效果。

实现方式:篇幅原因,下篇再论。

小结

以上讲述的就是集群或分布式环境下,session的5种处理策略。其中就应用广泛性而言,第三种方式,也就是基于第三方缓存框架共享session,应用的最为广泛,无论是效率还是扩展性都很好。而Terracotta作为一个JVM级的开源群集框架,不仅提供HTTP Session复制,它还能做分布式缓存,POJO群集,跨越群集的JVM来实现分布式应用程序协调等,也值得学习一下。

The above is the detailed content of Introduction to session processing methods in Linux cluster/distributed environment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

See all articles