A Cluster Multi-Node Caching Method and Device
By building cache collections in the memory of the service node and using the message queue broadcast mode to realize memory cache, the problem of service dependence on redis middleware is solved, lightweight and high scalability is achieved, the server's high concurrency capability is improved and cost-saving.
Patent Information
- Application Number
- CN202210876532.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-25
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2042-07-25
AI Technical Summary
In the prior art, service dependence on redis cache middleware leads to high reliability and high performance implementation dependencies, and cannot perform infinite expansion without relying on middleware, affecting the lightweight and high scalability of the service.
By building a cache collection in the memory of the service node, using the message queue broadcast mode to send data change messages to the upstream service cluster. The downstream service cluster automatically creates a message queue and binds message routing when each node service starts, implements memory cache, supports horizontal expansion, and avoids dependence on cache middleware.
It realizes lightweight services, does not rely on cache middleware, supports horizontal expansion, improves the server's high concurrency capabilities, and saves physical machine costs.
Smart Images

Figure CN115242811B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of database storage, and more specifically, to a cluster multi-node caching method and device. Background Art
[0002] With the popularization of the Internet and the continuous increase of network users, the requirements for high availability of services are also getting higher and higher, mainly including: high reliability, high performance, and high scalability. High reliability is generally solved by deploying clusters. The traditional method for high performance is to use redis caching to improve response efficiency. However, in this way, the service must rely on the redis middleware. When redis fails, the service will become unavailable. The service is not lightweight enough, and it is also impossible to infinitely expand the application service without considering the middleware. Summary of the Invention
[0003] In view of this, the present invention provides the following technical solutions:
[0004] A cluster multi-node caching method includes:
[0005] Multiple service nodes respectively obtain first service messages from their own corresponding unique message queues, and the first service messages are received by a message switch from an upstream service node and synchronized to each message queue;
[0006] After each service node receives the first service message, it stores the corresponding data in a cache set pre-constructed in memory, and the cache set is constructed by the service node in its own memory.
[0007] Optionally, before the multiple service nodes respectively obtain the first service messages from their own corresponding unique message queues, it further includes:
[0008] Each service node pre-creates its own corresponding message queue, binds the created message queue to the message switch, and listens to the corresponding message queue.
[0009] Optionally, the step that each service node stores the corresponding data in the pre-constructed cache set after receiving the first service message includes:
[0010] After each service node receives the first service message, it processes the information in the first service message to obtain data to be cached;
[0011] The data to be cached is stored in the pre-constructed cache set.
[0012] Optionally, the construction of the cache set includes:
[0013] The service node creates a doubly linked list in its own memory, which stores multiple pieces of cached data; during operation, the cached data that is accessed or called will be automatically added to the head of the doubly linked list.
[0014] Optionally, the cached data stored in the cache set has an expiration time attribute, and the method further includes:
[0015] The service node implements expiration cleaning of the cached data based on the time when the cached data is stored in the node and the expiration time of the cached data.
[0016] Optionally, it further includes:
[0017] After the service node is started, a daemon thread is started for the cache set. Among them, one cache set has one daemon thread, and the daemon thread executes periodically or at a fixed time.
[0018] Optionally, it further includes:
[0019] Create a scheduled task, which is used to synchronize the in-memory cached data of each service node.
[0020] Optionally, it further includes:
[0021] Receive a first instruction, and perform a full refresh or a specified cache set refresh on the cache set based on the first instruction.
[0022] Optionally, the message exchange is a rabbitMQ message exchange of a non-matching routing key type.
[0023] A cluster multi-node caching device includes:
[0024] A message acquisition module, which is used to acquire a first service message from the unique message queue corresponding to itself. The first service message is received by the message exchange from the upstream service node and synchronized to each message queue;
[0025] A data caching module, which is used to store the corresponding data in a pre-constructed cache set after receiving the first service message. The cache set is constructed by the service node in its own memory.
[0026] As can be seen from the above technical solutions, compared with the prior art, the embodiments of the present invention disclose a cluster multi-node caching method and device. The method includes: multiple service nodes respectively obtain first service messages from their corresponding unique message queues, and the first service messages are received by the message switch from the upstream service nodes and synchronized to each message queue; after each service node receives the first service message, it stores the corresponding data in a cache set pre-constructed in the memory, and the cache set is constructed by the service node in its own memory. The above solution constructs a set of caches in the service memory. Through the message queue broadcast mode, when the upstream service cluster sends a data change message to the message router, the downstream service cluster automatically creates a message queue and binds the message router when each node service starts. After the node service receives the change message, it processes the relevant data and caches it in the memory, realizing a lightweight service that does not rely on cache middleware, constructs its own memory cache, supports horizontal expansion, improves the high concurrency ability of the server, and saves the physical machine cost at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only the embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained according to the provided drawings.
[0028] Figure 1 It is a flowchart of a cluster multi-node caching method disclosed in an embodiment of the present invention;
[0029] Figure 2 It is an architecture diagram for implementing cached data of cluster nodes disclosed in an embodiment of the present application;
[0030] Figure 3 It is a flowchart of another cluster multi-node caching method disclosed in an embodiment of the present application;
[0031] Figure 4 It is an example diagram of a cache model disclosed in an embodiment of the present application;
[0032] Figure 5 It is a schematic structural diagram of a cluster multi-node caching device disclosed in an embodiment of the present application;
[0033] Figure 6 It is a schematic structural diagram of an electronic device disclosed in an embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0034] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0035] Figure 1 It is a flowchart of a cluster multi-node caching method disclosed in an embodiment of the present invention. Refer to Figure 1 As shown, the cluster multi-node caching method may include:
[0036] Step 101: Multiple service nodes respectively obtain first service messages from their corresponding unique message queues, and the first service messages are received by the message switch from the upstream service node and synchronized to each message queue.
[0037] Among them, the message switch may be, but is not limited to, a rabbitMQ message switch that does not match the routing key type.
[0038] To better understand the implementation of the present application, first, the implementation architecture of the cluster multi-node caching method described in the embodiments of the present application will be introduced. Figure 2 It is an implementation architecture diagram of cluster node caching data disclosed in an embodiment of the present application. Combining Figure 2 As shown, the upstream service cluster may send messages to the message switch, and the message switch may synchronously transmit the received messages in parallel to multiple message queues; among them, one message queue corresponds to a unique downstream service node (corresponding to Figure 2 nodes 1, 2... n in the lower-middle box), and when each downstream service node monitors that there is a new message in its corresponding message queue, it acquires and executes.
[0039] In specific implementation, the basic implementation of the cluster multi-node caching method may include:
[0040] 1), Build a rabbitMQ cluster and start the service.
[0041] 2), Create a new rabbitMQ message switch, the exchange name is unique, and the type is not matching the routing key, so as to maximize the message push performance.
[0042] 3), The upstream service cluster acts as a producer. When there is a service change, it sends service messages to the specified message switch. Among them, the service messages may be, but are not limited to, messages in JSON or XML format, and may include basic attributes and service attributes. Message basic attributes such as: service type, operation type, sender, message time, etc.; message service attributes such as: service id, service data, etc.
[0043] 4) The user-oriented application service removes the dependence on the redis cache middleware and is deployed in multiple nodes, with each node being a service instance.
[0044] 5) In the downstream application service cluster, multiple nodes form a cluster through load balancing to ensure high reliability and high performance.
[0045] 6) When each application service instance starts the application, it automatically creates a message queue and uses the ip of the Docker container where the current instance is located as the suffix of the queue name to ensure the uniqueness of the queue for each application service instance.
[0046] 7) When the application service starts, it is necessary to assemble and initialize the business data into the memory cache.
[0047] 8) Bind the created queue to the message exchange created in step 2. Specifically, one MQ configuration instance can be instantiated according to the rabbitMQ parameters first, and then the Exchange, Routingkey, and Queue are specified. Through the configuration instance, the exchange and the queue can be bound.
[0048] 9) Each service instance creates a consumer (i.e., the downstream application service cluster), establishes a message channel, and listens to the message queue created by itself.
[0049] 10) For different business requirements, each service instance constructs a cache set in the memory. The cache set supports multi-threaded concurrency to ensure that there are no thread safety issues.
[0050] After the above steps, the cluster multi-node caching method can be put into use, that is, multiple service nodes (corresponding to Figure 2 the downstream nodes in) respectively obtain the first business message from the unique message queue corresponding to themselves.
[0051] Step 102: After each service node receives the first business message, it stores the corresponding data in the cache set previously constructed in the memory. The cache set is constructed by the service node in its own memory.
[0052] Combined with Figure 2, when the upstream service cluster sends business messages, each node in the downstream application service cluster will obtain the business messages from their respective queues. Then, it triggers the update of the cached set of data. Specifically, according to the information such as the business ID and operation type in the first business message, the corresponding business data can be assembled into the data model required by the caller and then cached. The specific assembly process may include: according to the business data structures required by different business scenarios, such as basic data types like integer, floating point, character, boolean, etc., and data forms such as objects and collections encapsulated by the basic data types. After encapsulating into data instances, they are then stored in the memory; and the data instances stored in the memory can be called through the form of an external API interface.
[0053] Among them, the maximum number of cached contents can be set separately for each business cache set. In implementation, a doubly linked list can be created in the cache set. For any cached content that is hit, it is added to the head of the linked list. When setting a value in the cache set, the check for the maximum number of cached contents is started, and the last exceeded part in the doubly linked list is deleted, which can implement the least recently used elimination principle.
[0054] Based on the foregoing content, a cache set has been constructed in the memory. Therefore, after receiving the first business message, the corresponding data can be stored in the cache set pre-constructed in the memory. Specifically, it may include: after each service node receives the first business message, the data to be cached is processed based on the information in the first business message; and the data to be cached is stored in the pre-constructed cache set. Thus, the caching of data is achieved without the need for a cache middleware.
[0055] The multi-node cache method of the cluster in this embodiment constructs a set of caches in the service memory. Through the message queue broadcast mode, when the upstream service cluster sends a data change message to the message router, the downstream service cluster automatically creates a message queue and binds it to the message router when each node service starts. After receiving the change message, the node service processes the relevant data and caches it in the memory, realizing a lightweight service that does not rely on a cache middleware, constructs its own memory cache by itself, supports horizontal expansion, improves the high concurrency ability of the server, and saves physical resources and costs at the same time.
[0056] Based on the content of the above embodiments, before the multiple service nodes respectively obtain the first business message from their respective unique message queues, it may further include: each service node pre-creates its own corresponding message queue, binds the created message queue to the message switch, and listens to the corresponding message queue.
[0057] The construction of the cache set may include: the service node creates a doubly linked list in its own memory, which stores multiple cache data; during operation, the accessed or called cache will be automatically added to the head of the doubly linked list.
[0058] Figure 3 It is a flowchart of another cluster multi-node caching method disclosed in the embodiments of the present application. The cache data stored in the cache set has an expiration time attribute. As shown in combination with Figure 3 The cluster multi-node caching method may include:
[0059] Step 301: Multiple service nodes respectively obtain the first service message from the unique message queue corresponding to themselves, and the first service message is received by the message switch from the upstream service node and synchronized to each message queue.
[0060] Step 302: After each service node receives the first service message, it stores the corresponding data in the cache set pre-constructed in the memory, and the cache set is constructed by the service node in its own memory.
[0061] Among them, after the service node starts, a daemon thread is started for the cache set. One cache set has one daemon thread, and the daemon thread executes periodically or at a fixed time. When using the cache, starting the daemon thread can ensure that there is only one daemon thread for one cache set, preventing the system from crashing due to too many threads. The daemon thread can be set to execute at a fixed time. When executing, by judging the current system time and the expiration time of the cache, if the current system time is after the expiration time, the cache is cleared; otherwise, it is not cleared.
[0062] Step 303: The service node implements the expiration cleaning of the cache data based on the time when the cache data is stored in the node and the expiration time of the cache data.
[0063] Setting the expiration time attribute in the cache object and putting in the current system time plus the cache valid time can achieve automatic cache expiration cleaning, and support setting the expiration time and never expiring. Figure 4 It is an example diagram of the cache model disclosed in the embodiments of the present application, which can be combined with Figure 4 to understand the foregoing related content.
[0064] The expiration cleaning of cache data is an important part of implementing the cache. Because if the cache data in the memory is not cleared, it will cause the memory occupation of the application program to continuously increase, eventually leading to memory overflow and possibly service downtime.
[0065] In other implementations, the cluster multi-node caching method may further include: creating a scheduled task, and the scheduled task is used to synchronize the memory cache data of each service node.
[0066] To ensure data consistency, a compensation mechanism is established, a timed task is created to synchronize the in-memory cache data of each node, and the ultimate consistency of the system is ensured. Meanwhile, the method may further include: receiving a first instruction, and performing a full refresh or a specified refresh on the cache set based on the first instruction. That is, for each cache set, an AIP interface for full refresh and specified cache data refresh is provided.
[0067] In an implementation of data synchronization, a second rabbitMQ message exchange can be created, enabling a single node within the downstream application service cluster to send messages to this exchange, thereby triggering data synchronization for all other nodes at the same level downstream.
[0068] The multi-node cache method for the cluster in this embodiment first realizes the lightweighting of application services, facilitating horizontal expansion when the service generates request pressure; secondly, the application service facing users eliminates the dependence on the redis middleware and constructs a set of cache systems to improve the high performance of the application service; at the same time, it can also solve the problems of data synchronization and data consistency between nodes within the cluster.
[0069] For the foregoing method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should be aware that the present invention is not limited by the described action sequence, because according to the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to the present invention.
[0070] In the above embodiments disclosed by the present invention, the method is described in detail. The method of the present invention can be implemented by means of various forms of devices. Therefore, the present invention also discloses a device, and specific embodiments are given below for detailed description.
[0071] Figure 5 It is a schematic structural diagram of a multi-node cache device for a cluster disclosed in an embodiment of the present application. Refer to Figure 5 As shown, the multi-node cache device 50 for the cluster may include:
[0072] A message acquisition module 501, configured to acquire a first service message from a unique message queue corresponding to itself, where the first service message is received by the message exchange from an upstream service node and synchronized to each message queue.
[0073] A data cache module 502, configured to store the corresponding data in a pre-constructed cache set after receiving the first service message, where the cache set is constructed by the service node in its own memory.
[0074] The cluster multi-node caching device described in this embodiment constructs a set of caches in the service memory. Through the message queue broadcast mode, when the upstream service cluster sends a data change message to the message router, the downstream service cluster automatically creates a message queue and binds this message router when each node service starts. After receiving the change message, the node service processes the relevant data and caches it in the memory, realizing a lightweight service that does not rely on cache middleware, constructs its own memory cache, supports horizontal expansion, improves the high concurrency ability of the server, and saves physical and cost at the same time.
[0075] For the specific implementation of the message acquisition module and the data caching module and other possible implementations, please refer to the relevant introduction of the corresponding content in the method embodiment, which will not be repeated here.
[0076] Any one of the above-mentioned cluster multi-node caching devices includes a processor and a memory. The message acquisition module, data caching module, etc. in the above-mentioned embodiments are all stored in the memory as program modules, and the corresponding functions are realized by the processor executing the above program modules stored in the memory.
[0077] The processor contains a kernel, and the kernel retrieves the corresponding program module from the memory. One or more kernels can be set, and the processing of return visit data is realized by adjusting the kernel parameters.
[0078] The memory may include non-permanent memory in a computer-readable medium, forms such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (flash RAM), and the memory includes at least one storage chip.
[0079] In an exemplary embodiment, a computer-readable storage medium is also provided, which can be directly loaded into the internal memory of the computer and contains software code. After the computer program is loaded and executed by the computer, it can implement the steps shown in any embodiment of the above-mentioned cluster multi-node caching method.
[0080] In an exemplary embodiment, a computer program product is also provided, which can be directly loaded into the internal memory of the computer and contains software code. After the computer program is loaded and executed by the computer, it can implement the steps shown in any embodiment of the above-mentioned cluster multi-node caching method.
[0081] Furthermore, an embodiment of the present invention provides an electronic device. Figure 6 It is a schematic structural diagram of an electronic device disclosed in an embodiment of the present application. See Figure 6As shown, the electronic device includes at least one processor 601, at least one memory 602 connected to the processor, and a bus 603; wherein, the processor and the memory complete communication with each other through the bus; the processor is used to call program instructions in the memory to execute the above-mentioned cluster multi-node caching method.
[0082] The various embodiments in this specification are described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. For the same or similar parts among the various embodiments, reference can be made to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple. For the relevant parts, reference can be made to the description in the method section.
[0083] It should also be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article or device including the said element.
[0084] The steps of the methods or algorithms described in connection with the embodiments disclosed herein can be implemented directly in hardware, software modules executed by a processor, or a combination of both. The software modules can be placed in a random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium well-known in the technical field.
[0085] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to those skilled in the art. The general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but will be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A cluster multi-node caching method, characterized in that, Including: Multiple service nodes respectively obtain first service messages from their corresponding unique message queues. The first service messages are received by the message switch from upstream service nodes when there are service changes in the upstream service nodes and synchronized to each message queue; After each service node receives the first service message, it processes the information in the first service message to obtain data to be cached; Store the data to be cached in a pre-constructed cache set, and realize data caching on the premise of avoiding setting up a cache middleware; The cache set is constructed by the service node in its own memory; Among them, the construction of the cache set includes: the service node creates a doubly linked list in its own memory, which stores multiple cache data; Create a second message switch to receive messages sent by a single node in the downstream application service cluster and trigger data synchronization of all other nodes at the same level downstream.
2. The cluster multi-node caching method according to claim 1, wherein Before the multiple service nodes respectively obtain the first service messages from their corresponding unique message queues, it also includes: Each service node pre-creates its corresponding message queue, binds the created message queue to the message switch, and listens to the corresponding message queue.
3. The cluster multi-node caching method according to claim 1, characterized in that, For the doubly linked list, during the working process, the cached data that is accessed or called will be automatically added to the head of the doubly linked list.
4. The cluster multi-node caching method according to claim 3, wherein The cached data stored in the cache set has an expiration time attribute, and the method also includes: The service node realizes expiration cleaning of the cached data based on the time when the cached data is stored in the node and the expiration time of the cached data.
5. The cluster multi-node caching method according to claim 1, wherein It also includes: After the service node is started, start a daemon thread for the cache set. Among them, one cache set has one daemon thread, and the daemon thread executes periodically or at a fixed time.
6. The cluster multi-node caching method according to claim 1, further comprising: Create a timing task, and the timing task is used to synchronize the memory cache data of each service node.
7. The cluster multi-node caching method according to claim 1, wherein It also includes: Receive a first instruction, and perform a full refresh or a specified cache set refresh on the cache set based on the first instruction.
8. The cluster multi-node caching method according to claim 1, wherein The message switch is a rabbitMQ message switch of a non-matching routing key type.
9. A cluster multi-node cache device, characterized in that, Including: A message obtaining module, which is used to obtain first service messages from its corresponding unique message queue. The first service messages are received by the message switch from upstream service nodes when there are service changes in the upstream service nodes and synchronized to each message queue; A data caching module, which is used to process the information in the first service message to obtain data to be cached after receiving the first service message; Store the data to be cached in a pre-constructed cache set, and realize data caching on the premise of avoiding setting up a cache middleware; The cache set is constructed by the service node in its own memory; Among them, the construction of the cache set includes: the service node creates a doubly linked list in its own memory, which stores multiple cache data; The cluster multi-node caching device further includes: a second message switch, which is used to receive messages sent by a single node in the downstream application service cluster and trigger data synchronization of all other nodes at the same level downstream.
Citation Information
Patent Citations
Data processing method and device, electronic equipment and storage medium
CN111580993A
Cache data acquisition method, device and equipment and storage medium
CN112559570A