Method and device for realizing shared session based on cache
By adopting the master-slave architecture and sentinel mode in the Redis cluster, the shortcomings of Session state management in Session authentication are solved, efficient access to Session and high availability of applications are achieved, and the availability and security of the system are improved.
Patent Information
- Application Number
- CN202510661959.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-22
- Publication Date
- 2025-08-08
AI Technical Summary
In the prior art, the Session authentication method of the client and the server cannot effectively manage the Session status, resulting in the Session always being valid before it expires, and the permissions cannot be abolished or changed, which poses a security risk.
Adopting the master-slave architecture and sentinel mode, the main cache node is responsible for storing cached data and copying it to the slave cache node. The sentinel node monitors the node status and automatically elects a new main cache node when the main cache node goes down to achieve failure recovery and data consistency.
It realizes efficient access to Session and high availability of applications, solves Session sharing problems in distributed deployment, and improves system availability and security.
Smart Images

Figure CN120455541A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of cache deployment, and specifically provides a method and device for implementing shared session based on cache. Background Art
[0002] The current status of client and server session authentication is divided into the following three categories: Basic Authentication based on password, Token-based Authentication based on token, and Session-Based Authentication based on session.
[0003] With password-based authentication (Basic Authentication), the client sends a username and password to the server via an HTTP header. Because usernames and passwords are encoded in Base64, they are easily intercepted and are not recommended for transmitting sensitive information.
[0004] Token-based authentication, such as JWT (JSON Web Tokens), works on the principle that after the server verifies the client, it generates a JWT (JSON Web Tokens) and sends it to the client, and the client carries this token in subsequent requests. Compared with Basic Authentication, JWT (JSON Web Tokens) is more secure because it can contain signatures to prevent tampering. It is a concise, URL-safe statement specification for transmitting security information between two parties. As an open standard (RFC 7519), JWT defines a concise, self-contained method for securely transmitting information between communicating parties in the form of Json objects. Because of the existence of digital signatures, this information is trustworthy, and JWT can be signed using the HMAC algorithm or the RSA public-private key pair.
[0005] Session-Based Authentication: After authenticating the client, the server creates a session and returns a session identifier (such as a session ID). The client carries this identifier in subsequent requests. This relies on secure session management, such as using HTTPS to encrypt the transmission of the session ID.
[0006] All three of the above methods have flaws. Since the server does not save the session state, it is impossible to revoke a token or change the token's permissions during use. In other words, the generated session will remain valid until it expires. Summary of the Invention
[0007] The present invention aims to address the above-mentioned deficiencies in the prior art and provides a highly practical method for implementing shared sessions based on cache.
[0008] A further technical task of the present invention is to provide a reasonably designed, safe and applicable cache-based shared session device.
[0009] The technical solution adopted by the present invention to solve its technical problem is:
[0010] A method for implementing shared sessions based on cache first uses a master-slave architecture, with one master and multiple slaves plus a sentinel mode. The master cache node is responsible for storing cached data and copying the cached data to other slave cache nodes, while the slave cache nodes are responsible for reading the cached data. When the master cache node service goes down, the slave cache nodes will elect a new master cache node.
[0011] Furthermore, in the master-slave mode, when the primary cache node goes down, the slave cache node acts as the primary cache node and continues to provide services, using the sentinel mode to achieve automatic fault recovery to obtain cache data.
[0012] Furthermore, the sentinel mode is responsible for monitoring whether the Redis master node and slave node processes are working properly. When a node goes down, the sentinel is responsible for sending a message notification to the administrator and automatically transferring it to the slave node.
[0013] Furthermore, when defining the master-slave mode, the Master is the primary cache node and the Slave is the secondary cache node. Data replication is one-way, only from the Master node to the Slave node. Once the Master cache node goes down, the Slave cache node will replace it as the new Master cache node, and the cached data is consistent.
[0014] Furthermore, when defining a sentinel cluster, the sentinel node consists of two parts: a sentinel node and a data node. The sentinel node consists of one or more sentinel nodes. The sentinel node is a special Redis node that does not store data.
[0015] Among the data nodes, the master node and the slave node are both data nodes;
[0016] Access to the redis cluster data is through the sentinel cluster, which monitors the entire redis cluster.
[0017] Furthermore, when defining the interaction relationship between the sentinel node and the master and slave cache nodes, each sentinel node sends a PING command to all its master cache nodes, slave cache nodes, and other sentinel instances at a frequency of once per second. If the time from an instance to the last valid response to the PING command exceeds a certain time, the instance will be marked as subjectively offline by the sentinel.
[0018] Furthermore, if a primary cache node is marked as subjectively offline, all sentinel nodes that are monitoring this primary cache node must confirm once per second whether the primary cache node has indeed entered the subjectively offline state.
[0019] If a master server is marked as subjectively offline and a sufficient number of sentinels agree with this judgment within a specified time range, then the master cache node is marked as objectively offline.
[0020] Furthermore, the sentinel and other sentinel nodes negotiate the status of the master cache node. If the master cache node is down, they vote to automatically select a new master cache node and point the remaining slave cache nodes to the new master cache node for cache data replication.
[0021] When there are not enough sentinel nodes to agree that the primary cache node is offline, the objective offline status of the primary cache node will be removed. When the primary cache node returns a valid reply to the sentinel node's PING command again, the subjective offline status of the primary cache node will be removed.
[0022] A session sharing device based on cache, comprising: at least one memory and at least one processor;
[0023] The at least one memory is configured to store a machine-readable program;
[0024] The at least one processor is configured to call the machine-readable program to execute a method for implementing a shared session based on cache.
[0025] Compared with the prior art, the method and device for implementing shared session based on cache of the present invention have the following outstanding beneficial effects:
[0026] The present invention can solve the problem of session sharing in distributed deployment of business applications, and through the sentinel mode cluster deployment of Redis, session access is efficient and the application is highly available. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0028] Attachment Figure 1 This is a Redis cluster effect diagram in a cache-based shared session method;
[0029] Attachment Figure 2 It is a master-slave mode structure diagram in a cache-based shared session method. DETAILED DESCRIPTION
[0030] In order to enable those skilled in the art to better understand the solutions of the present invention, the present invention will be further described in detail below in conjunction with specific embodiments. Obviously, the embodiments described are only some embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0031] A best embodiment is given below:
[0032] like Figure 1 As shown in the figure, this embodiment implements a cache-based shared session method designed as a master-slave architecture with one master and multiple slaves in a sentinel mode. The master cache node is responsible for storing cached data and replicating it to other slave cache nodes, while the slave cache nodes are responsible for reading cached data. When the master cache node service fails, the slave cache nodes will elect a new master cache node, thereby ensuring high service availability. Only a read-write separation architecture can easily achieve horizontal scalability and support high concurrency requests.
[0033] Redis is a key-value storage system. It provides a rich set of data structures, including strings, lists, sets, sorted sets, and hashes. These data types support push / pop, add / remove, intersection, union, and difference operations, as well as more atomic operations. Furthermore, Redis supports various sorting methods. Like Memcached, data is cached in memory for efficiency.
[0034] Since Redis is an in-memory, non-relational database, it naturally supports high concurrency, and access to cached data is very efficient.
[0035] In the master-slave model, if the primary cache node fails, the secondary cache node can take over as the primary cache node and continue providing services. However, there is a problem: the IP address of the primary cache node has changed. At this time, the application service still uses the original primary cache node address to access the cached data, resulting in an inability to retrieve cached data. This is where the sentinel model plays a key role, enabling automated fault recovery.
[0036] Sentinel monitors the proper functioning of the Redis master and slave processes. If a node fails, Sentinel notifies the administrator and automatically switches to a slave. If a failover occurs, the master node's address changes, but the application server remains unaware and does not need to change its access address, as Sentinel interacts with the application server. Sentinel mode effectively addresses failover and takes high availability to a new level.
[0037] like Figure 2 As shown in the figure, when defining a master-slave model, the master is the primary cache node, and the slave is the secondary cache node. Data replication is unidirectional, only from the master node to the secondary node. If the master cache node fails, a secondary cache node will take over as the new master cache node. The cached data remains consistent, without affecting client access.
[0038] When defining a sentinel cluster, the sentinel node consists of two parts, the sentinel node and the data node. The sentinel node consists of one or more sentinel nodes. The sentinel node is a special Redis node that does not store data.
[0039] The master node and the slave node in the data node are both data nodes. Access to the data of the redis cluster is through the sentinel cluster, which monitors the entire redis cluster.
[0040] When defining the interaction between Sentinel nodes and master and slave cache nodes, each Sentinel node sends a PING command to all its master and slave cache nodes, as well as other Sentinel instances, at a frequency of once per second. If an instance has not responded to a valid PING command for more than a certain period of time, it will be marked as subjectively offline by the Sentinel.
[0041] If a primary cache node is marked as subjectively offline, all sentinel nodes monitoring the primary cache node will confirm once per second whether the primary cache node has indeed entered the subjective offline state. If a primary server is marked as subjectively offline and a sufficient number of sentinels agree with this judgment within a specified time range, the primary cache node is marked as objectively offline.
[0042] Sentinels negotiate the status of the primary cache node with other Sentinel nodes. If the primary cache node is down, they vote to automatically elect a new primary cache node. Remaining slave cache nodes are directed to the new primary cache node for cache data replication. If a sufficient number of Sentinel nodes no longer agree to the primary cache node's offline status, the primary cache node's objective offline status is removed. When the primary cache node returns a valid response to the Sentinel node's ping command, the primary cache node's subjective offline status is removed.
[0043] Based on the above method, a cache-based shared session device in this embodiment includes: at least one memory and at least one processor;
[0044] The at least one memory is configured to store a machine-readable program;
[0045] The at least one processor is configured to call the machine-readable program to execute a method for implementing a shared session based on cache.
[0046] The above-mentioned specific implementation methods are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above-mentioned specific implementation methods. Any technical solutions that conform to the above-mentioned specific implementation methods of the present invention and any appropriate changes or substitutions made thereto by ordinary technicians in the relevant technical field shall fall within the patent protection scope of the present invention.
[0047] While embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions, and variations may be made to these embodiments without departing from the principles and spirit of the invention, and that the scope of the invention is defined by the appended claims and their equivalents.
Claims
1. A method for implementing shared session based on cache, characterized in that: First, use a master-slave architecture, with one master and multiple slaves plus a sentinel mode. The master cache node is responsible for storing cached data and copying the cached data to other slave cache nodes, while the slave cache node is responsible for reading the cached data. When the master cache node service goes down, the slave cache node will elect a new master cache node.
2. A method for implementing shared session based on cache according to claim 1, characterized in that: In the master-slave mode, when the primary cache node goes down, the slave cache node acts as the primary cache node and continues to provide services. The sentinel mode is used to achieve automatic fault recovery and obtain cache data.
3. The method for implementing shared session based on cache according to claim 2, characterized in that: Sentinel mode is responsible for monitoring whether the Redis master node and slave node processes are working properly. When a node goes down, the sentinel is responsible for sending a message notification to the administrator and automatically transferring the work to the slave node.
4. The method for implementing shared session based on cache according to claim 3, characterized in that: When defining the master-slave mode, the Master is the primary cache node and the Slave is the secondary cache node. Data replication is one-way, only from the master node to the slave node. Once the master cache node goes down, the slave cache node will replace it as the new master cache node, and the cached data will be consistent.
5. The method for implementing shared session based on cache according to claim 4, characterized in that: When defining a sentinel cluster, the sentinel node consists of two parts: the sentinel node and the data node. The sentinel node consists of one or more sentinel nodes. The sentinel node is a special Redis node that does not store data. Among the data nodes, the master node and the slave node are both data nodes; Access to the redis cluster data is through the sentinel cluster, which monitors the entire redis cluster.
6. The method for implementing shared session based on cache according to claim 5, characterized in that: When defining the interaction relationship between Sentinel nodes and master and slave cache nodes, each Sentinel node sends a PING command to all its master and slave cache nodes and other Sentinel instances once per second. If an instance has not responded to the last valid PING command for more than a certain period of time, the instance will be marked as subjectively offline by the Sentinel.
7. The method for implementing shared session based on cache according to claim 6, characterized in that: If a primary cache node is marked as subjectively offline, all sentinel nodes that are monitoring this primary cache node must confirm once per second whether the primary cache node has indeed entered the subjectively offline state. If a master server is marked as subjectively offline and a sufficient number of sentinels agree with this judgment within a specified time range, then the master cache node is marked as objectively offline.
8. The method for implementing shared session based on cache according to claim 7, characterized in that: The sentinel and other sentinel nodes negotiate the status of the master cache node. If the master cache node is down, they vote to automatically select a new master cache node and point the remaining slave cache nodes to the new master cache node for cache data replication. When there are not enough sentinel nodes to agree that the primary cache node is offline, the objective offline status of the primary cache node will be removed. When the primary cache node returns a valid reply to the sentinel node's PING command again, the subjective offline status of the primary cache node will be removed.
9. A session sharing device based on cache, characterized in that: include: at least one memory and at least one processor; The at least one memory is configured to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 8.