This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2945|Reply: 0

Nginx load balancing addresses the backend SESSION single point issue

[Copy link]
Posted on 1/10/2023 8:38:39 PM | | | |
Requirements: Use Nginx to build a reverse proxy, responsible for scheduling all requests, the backend is developed by ASP.NET MVC, and deployed to multiple different servers, forming a backend clusterSession information cannot be shared, resulting in some requests not being processed correctly。 The following problems will arise:





ASP.NET The website can be referred to as follows:

asp.net session sharing (via StateServer)
https://www.itsvse.com/thread-6613-1-1.html

. Net distributed architecture: Session sharing based on Redis
https://www.itsvse.com/thread-5189-1-1.html

Commonly used solutions are as follows:

Use the database to store the SESSION

Since each server needs to use the same session, we can store the session in the same database, every time we access, we go to the database to check whether there is this session or whether this session has expired, and then we can synchronize the session of multiple servers;

Merit:Using this method is simple, convenient, and easy to get started;

Shortcoming:Using the database to synchronize sessions will increase the IO of the database and increase the burden on the database. At the same time, each access needs to intercept requests and query the database, resulting in an additional layer of access and wasted database session time.


Use a caching mechanism such as Memcache or Redis to store the SESSION

Using distributed caching mechanisms such as memcache or redis to store session data is a popular solution for load balancing and synchronous sessions in many large-scale projects. Its principle is that the project uses the memcache or redis cache in the same place, when the user logs in, the session will be stored in the cache, and then no matter which server of the project is accessed, the session cache will be obtained from the same place, so that session synchronization is easily realized;

Merit:Using cache to synchronize sessions will not increase the burden on the database, nor do you need to manually judge whether the session exists or expires, eliminating some business logic.

Shortcoming:memcache or redis divides memory into many specifications of storage blocks, and there are blocks with sizes, which also determines that memcache or redis cannot fully utilize memory, which will produce memory fragmentation, and if the storage blocks are insufficient, memory overflow will also occur.

Leverage the ip_hash pattern in Nginx

This technique, also known as session keeping, is the ip_hash technology in nginx that allows you toRequests from a certain IP address are pinned to the same backend application server, so that a client and a backend under this IP can establish a stable session.

(But there is also a disadvantage, if the operator's network is more volatile and unstable,Egress IP is dynamicYes, there will be problems with this method. )

The test method is to open two sites based on docker,You need to create a new index.html file under the /data/testsite/a and /data/testsite/b directories, respectively, the command is as follows:

Access through a browser as shown below:



If you create a new nginx container based on Docker, without using ip_hash technology, the request can be sent to different backend servers, as shown in the figure below:



Using ip_hash technology, create a new /data/testsite/nginx.conf file with the following configuration:

docker startup command is as follows:




Cookie-based session keeping, which can be consulted in the sticky module, omitted

Nginx configuration:The hyperlink login is visible.

(End)




Previous:【Practical Action】Use Docker to build a Gitlab private repository
Next:IIS URL Rewrite Module Rule Clearing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com