Cache high optimization scheme under micro service
By introducing Redis cache module into the microservice architecture, using technologies such as Bloom filters and distributed locks, cache penetration, avalanches, breakdown and concurrency are solved, efficient cache management and data consistency are achieved, and data consistency is improved. Development efficiency and availability of cache systems.
Patent Information
- Application Number
- CN202410993000.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-23
- Publication Date
- 2024-11-29
AI Technical Summary
In the microservice architecture, problems such as cache penetration, cache avalanche, cache breakdown and cache concurrency make it difficult to achieve cache management and data consistency, resulting in developers needing to write a large amount of redundant code to reduce development efficiency.
Provide a high-optimization solution for cache under microservices, including Redis cache module, through technologies such as Bloom filter, distributed lock, secondary cache, evenly allocated Redis key failure time, message middleware and distributed lock + timestamp. , realizes the functions of anti-cache penetration, anti-cache avalanche, anti-cache breakdown and anti-cache concurrency, and is packaged as a configurable component.
Effectively avoid developers writing redundant code, improve development efficiency, solve cache penetration, avalanche, breakdown and concurrency problems through efficient data caching and query functions, improve the availability and reusability of the cache system, and reduce the technology threshold.
Smart Images

Figure CN119046320A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of microservice technology, and specifically is a cache high optimization solution under microservice. Background Art
[0002] As the business develops, the architecture of monolithic applications can no longer support the huge backend business, so microservices on the server side are an inevitable trend. In the microservice architecture, as the service splitting deepens, each microservice may use cache independently to optimize performance. Since there are multiple services and each service may have its own cache requirements, managing these cache instances and ensuring data consistency becomes a challenge.
[0003] At present, we often use Redis to store data in daily development. However, in the process of using Redis, we usually encounter problems such as cache penetration, cache avalanche, cache breakdown, cache concurrency, etc. At present, the cache function used by most business systems only uses the functions of caching data and querying data, without considering cache penetration, cache avalanche, cache breakdown, cache concurrency and other problems. Business systems that take these problems into consideration only consider some of them, and do not take them into consideration as a whole. In addition, during development, technicians need to write a large amount of redundant template code, which makes the development work have high technical requirements for technicians. Compiling a large number of code templates will also distract developers and reduce development efficiency, so they need to be improved and optimized. Summary of the invention
[0004] The purpose of the present invention is to provide a highly optimized cache solution under microservices to solve the problems raised in the above background technology.
[0005] In order to achieve the above object, the present invention provides the following technical solutions: a high-optimization cache solution under microservices, wherein the high-optimization cache solution under microservices includes a Redis cache module, wherein the Redis cache module includes an anti-cache penetration function module, an anti-cache avalanche function module, an anti-cache breakdown function module, an anti-cache concurrency function module and a new extension function module;
[0006] The anti-cache penetration function module is implemented through a Bloom filter solution and a cache empty object solution, the anti-cache avalanche function module is implemented through a distributed lock solution, a secondary cache (EhCache+Redis) solution and a scheme for evenly distributing the expiration time of Redis keys, the anti-cache breakdown function module is implemented through a distributed lock solution, and the anti-cache concurrency function module is implemented through a message middleware solution and a distributed lock+timestamp solution.
[0007] Preferably, the Redis cache module is defined by a configurable cache query method module and a configurable cache update method module, and the configurable cache query method module and the configurable cache update method module are defined by parameters keyAll, expire, redisLevelConfig, clazz and cacheLoadBack, and the definition method of the configurable cache query method module is:
[0008] S1, the staff first determines the redisLevelConfig parameter. When the redisLevelConfig parameter is empty, a general cache query operation is performed;
[0009] S2, the staff queries the cache. If there is data in the cache, the result is returned. If there is no data in the cache, the staff queries the database. If there is data in the database, the staff updates the cache and returns the result. If there is no data in the database, the staff returns a null value.
[0010] S3: When the redisLevelConfig parameter is not empty and the anti-cache penetration function module is configured in the redisLevelConfig parameter, the Bloom filter solution is used to determine if the key exists, and the next step is performed. Otherwise, null is returned.
[0011] S4: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache avalanche, data is returned through a method based on the mechanism implemented in the distributed lock solution and the secondary cache (EhCache+Redis) solution;
[0012] S5: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache breakdown, data is returned through a method based on the mechanism in the distributed lock solution;
[0013] S6: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with an extended function, the extended function is implemented through the new extended function module and the new function data is returned.
[0014] Preferably, the configurable cache update method module adds and updates cache data according to the redisLevelConfig parameter configuration, so that the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown and scalability;
[0015] The configurable cache query method module can return query results, and through configuration, the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown, and is scalable.
[0016] Preferably, the Bloom filter solution is implemented as follows:
[0017] A1, first define the global Bloom filter;
[0018] A2, initialize the global Bloom filter and initialize all the data to be queried in the system into the global Bloom filter;
[0019] A3: When the cache module is configured with the anti-cache penetration function, it will be judged through the Bloom filter. When the key value does not exist in the Bloom filter, it will return directly to block the traffic. When the key value exists, the normal logic will be performed.
[0020] Preferably, the caching empty object solution caches the returned empty object after a storage layer miss, and sets a shorter expiration time.
[0021] Preferably, the distributed lock solution is implemented as follows:
[0022] B1, first query the cache. If there is data in the cache, the result is returned, indicating that the cache has been set by other threads. If there is no data in the cache, try to obtain the distributed lock;
[0023] B2, when the lock is acquired successfully, the database is queried, and if there is data in the database, the cache is updated and the result is returned. If there is no data in the database, an empty result is returned;
[0024] B3, when the lock acquisition fails, it sleeps for a while and then restarts the operation in B1.
[0025] Preferably, the secondary cache (EhCache+Redis) solution includes EhCache cache and Redis cache, the EhCache is used as the first-level cache, and the Redis is used as the second-level cache;
[0026] The implementation method of the secondary cache (EhCache+Redis) solution is:
[0027] C1, first set up the first-level cache EhCache and the second-level cache Redis;
[0028] C2, query data, first query the first-level cache, if there is data in the first-level cache, the result is returned, if there is no data, query the second-level cache;
[0029] C3, query the second-level cache. If there is data in the second-level cache, update the first-level cache and return the result. If the data does not exist, query the database.
[0030] C4, queries the database. When the data exists, it updates the first-level cache and the second-level cache. When there is no data, it returns empty.
[0031] Preferably, the scheme for evenly distributing the expiration time of Redis keys can avoid the expiration of a large number of keys within a period of time by setting different expiration times for cached keys according to functional modules.
[0032] Preferably, the message middleware solution can be processed by the message middleware to serialize the parallel write;
[0033] The distributed lock + timestamp solution changes parallel reading and writing into serial reading and writing through distributed locks, and then uses timestamps to ensure that key operations are executed in sequence.
[0034] Preferably, the new extended function module realizes the extension of new functions by configuring the dynamic extended attributes and adding new attributes in the redisLevelConfig class.
[0035] The beneficial effects of the present invention are as follows:
[0036] 1. The present invention defines the interface method of using cache and adds corresponding annotations, so that developers can easily add cache support to the interface method without worrying about the underlying cache implementation details, effectively avoiding developers from writing and debugging redundant template code, allowing developers to focus more on their own business logic to improve development efficiency, and through efficient data caching and data query functions, providing solutions to problems such as cache penetration, cache avalanche, cache breakdown, cache concurrency, etc., and encapsulating these solutions into components, so that the cache system has high availability and reusability through configuration.
[0037] 2. The present invention encapsulates the cache system as Spring Boot Starter, which can be easily introduced and used in different microservice projects. Each microservice only needs to introduce its Maven dependency to quickly integrate the cache function, thereby improving portability, supporting custom configuration, and meeting personalized needs. The cache system can be flexibly configured with the business and uniformly managed. In addition, Starter has built-in anti-cache penetration, anti-cache avalanche, anti-cache breakdown, and anti-cache concurrency configurations, which improves the development efficiency of developers and lowers the technical threshold. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 This is a functional architecture diagram of the present invention;
[0039] Figure 2 This is the anti-cache penetration module-Bloom filter diagram of the present invention;
[0040] Figure 3 The anti-cache penetration module of the present invention - cache empty object graph;
[0041] Figure 4 The anti-cache avalanche module of the present invention - distributed lock graph;
[0042] Figure 5 This is the anti-cache avalanche module of the present invention - the secondary cache (EhCache+Redis) diagram;
[0043] Figure 6 This is a diagram of the anti-cache concurrency module-message middleware of the present invention. DETAILED DESCRIPTION
[0044] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0045] like Figures 1 to 6 As shown, an embodiment of the present invention provides a high-optimization cache solution under microservices, a high-optimization cache solution under microservices includes a Redis cache module, and the Redis cache module includes an anti-cache penetration function module, an anti-cache avalanche function module, an anti-cache breakdown function module, an anti-cache concurrency function module and a new extension function module;
[0046] The anti-cache penetration function module is implemented through the Bloom filter solution and the cache empty object solution. The anti-cache avalanche function module is implemented through the distributed lock solution, the secondary cache (EhCache+Redis) solution and the evenly distributed Redis key expiration time solution. The anti-cache breakdown function module is implemented through the distributed lock solution. The anti-cache concurrency function module is implemented through the message middleware solution and the distributed lock + timestamp solution.
[0047] By defining the interface methods that use the cache and adding corresponding annotations, developers can easily add cache support to the interface methods without having to worry about the underlying cache implementation details, effectively avoiding developers from writing and debugging redundant template code, allowing developers to focus more on their own business logic to improve development efficiency. In addition, through efficient data caching and data query functions, it provides solutions to problems such as cache penetration, cache avalanche, cache breakdown, and cache concurrency, and encapsulates these solutions into components. Through configuration, the cache system has the ability to be highly available and reusable.
[0048] By encapsulating the cache system as Spring Boot Starter, it can be easily introduced and used in different microservice projects. Each microservice only needs to introduce its Maven dependency to quickly integrate the cache function, which improves portability, supports custom configuration, and meets personalized needs. The cache system can be flexibly configured with the business and uniformly managed. In addition, Starter has built-in anti-cache penetration, anti-cache avalanche, anti-cache breakdown, and anti-cache concurrency configurations, which improves the development efficiency of developers and lowers the technical threshold.
[0049] like Figure 1 As shown, the Redis cache module is defined by a configurable cache query method module and a configurable cache update method module. The configurable cache query method module and the configurable cache update method module are defined by parameters keyAll, expire, redisLevelConfig, clazz, and cacheLoadBack. The definition method of the configurable cache query method module is:
[0050] S1, the staff first determines the redisLevelConfig parameter. When the redisLevelConfig parameter is empty, a general cache query operation is performed;
[0051] S2, the staff queries the cache. If there is data in the cache, the result is returned. If there is no data in the cache, the staff queries the database. If there is data in the database, the staff updates the cache and returns the result. If there is no data in the database, the staff returns a null value.
[0052] S3: When the redisLevelConfig parameter is not empty and the anti-cache penetration function module is configured in the redisLevelConfig parameter, the Bloom filter solution is used to determine if the key exists, and the next step is performed. Otherwise, null is returned.
[0053] S4: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache avalanche, data is returned through a method based on the mechanism implemented in the distributed lock solution and the secondary cache (EhCache+Redis) solution;
[0054] S5: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache breakdown, data is returned through a method based on the mechanism in the distributed lock solution;
[0055] S6: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with an extended function, the extended function is implemented through the new extended function module and the new function data is returned.
[0056] The parameters are described as follows: keyAll: key, expire: expiration time, redisLevelConfig: anti-cache penetration, anti-cache avalanche, anti-cache breakdown, extended function configuration parameters, clazz: the serialization class of the returned result, serializes the query result into the specified data format, cacheLoadBack: a functional interface, the caller can implement this interface, query the database as needed, and obtain the data to be cached.
[0057] like Figure 1 As shown, the configurable cache update method module adds and updates cache data according to the redisLevelConfig parameter configuration, so that the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown and scalability. For example, redisLevelConfig configures the ability to prevent cache concurrency, and concurrent cache updates are implemented through the message middleware mechanism;
[0058] Configurable cache query method module, which can return query results, and through configuration, the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown, and scalability
[0059] like Figure 2 As shown, the implementation of the Bloom filter solution is:
[0060] A1, first define the global Bloom filter;
[0061] A2, initialize the global Bloom filter and initialize all the data to be queried in the system into the global Bloom filter;
[0062] A3: When the cache module is configured with the anti-cache penetration function, it will be judged through the Bloom filter. When the key value does not exist in the Bloom filter, it will return directly to block the traffic. When the key value exists, the normal logic will be performed.
[0063] Among them, the Bloom filter stores all possible query data in hash form, verifies it first at the control layer, and discards it if it does not meet the requirements, thus avoiding query pressure on the underlying storage system.
[0064] like Figure 3 As shown in the figure, the empty object cache solution caches the returned empty object after a storage layer miss, and sets a shorter expiration time.
[0065] If the data is accessed again within the expiration time, it will be obtained from the cache, thus protecting the backend data source.
[0066] like Figure 4 As shown in the figure, the distributed lock solution is implemented as follows:
[0067] B1, first query the cache. If there is data in the cache, the result is returned, indicating that the cache has been set by other threads. If there is no data in the cache, try to obtain the distributed lock;
[0068] B2, when the lock is acquired successfully, the database is queried, and if there is data in the database, the cache is updated and the result is returned. If there is no data in the database, an empty result is returned;
[0069] B3, when the lock acquisition fails, it sleeps for a while and then restarts the operation in B1.
[0070] Among them, when the query cache result is empty, a distributed lock is acquired to ensure that only one thread updates the cache at the same time, avoiding a large number of requests directly accessing the database.
[0071] like Figure 5 As shown, the secondary cache (EhCache+Redis) solution includes EhCache cache and Redis cache, EhCache is used as the first-level cache, and Redis is used as the second-level cache;
[0072] The implementation of the secondary cache (EhCache+Redis) solution is as follows:
[0073] C1, first set up the first-level cache EhCache and the second-level cache Redis;
[0074] C2, query data, first query the first-level cache, if there is data in the first-level cache, the result is returned, if there is no data, query the second-level cache;
[0075] C3, query the second-level cache. If there is data in the second-level cache, update the first-level cache and return the result. If the data does not exist, query the database.
[0076] C4, queries the database. When the data exists, it updates the first-level cache and the second-level cache. When there is no data, it returns empty.
[0077] like Figure 1 As shown in the figure, the scheme of evenly distributing the expiration time of Redis keys can avoid the expiration of a large number of keys in a period of time by setting different expiration times for cached keys according to functional modules.
[0078] like Figure 6 As shown, the message middleware solution can be processed by the message middleware to serialize parallel writes;
[0079] The distributed lock + timestamp solution changes parallel reading and writing into serial reading and writing through distributed locks, and then uses timestamps to ensure that key operations are executed in sequence.
[0080] Among them, the distributed lock + timestamp solution first assumes that system A key1{ValueA 1:00} and system B key1{ValueB 1:05}. If system B grabs the lock first and sets key1 to {ValueB 1:05}, then system A grabs the lock and finds that the timestamp of its key1 is earlier than the timestamp in the cache (1:00<1:05), then the set operation will not be performed.
[0081] like Figure 1 As shown in the figure, the new extended function module implements the extension of new functions by configuring the dynamic extended attributes and adding new attributes in the redisLevelConfig class.
[0082] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.
[0083] Although 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 the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A highly optimized cache solution for microservices. Features: The solution includes a Redis cache module, which includes an anti-cache penetration function module, an anti-cache avalanche function module, an anti-cache breakdown function module, an anti-cache concurrency function module and a new extension function module; The anti-cache penetration function module is implemented through a Bloom filter solution and a cache empty object solution, the anti-cache avalanche function module is implemented through a distributed lock solution, a secondary cache (EhCache+Redis) solution and a scheme for evenly distributing the expiration time of Redis keys, the anti-cache breakdown function module is implemented through a distributed lock solution, and the anti-cache concurrency function module is implemented through a message middleware solution and a distributed lock+timestamp solution.
2. According to the cache high optimization solution under the microservice of claim 1, Features: The Redis cache module is defined by a configurable cache query method module and a configurable cache update method module. The configurable cache query method module and the configurable cache update method module are defined by parameters keyAll, expire, redisLevelConfig, clazz and cacheLoadBack. The definition method of the configurable cache query method module is: S1, the staff first determines the redisLevelConfig parameter. When the redisLevelConfig parameter is empty, a general cache query operation is performed; S2, the staff queries the cache. If there is data in the cache, the result is returned. If there is no data in the cache, the staff queries the database. If there is data in the database, the staff updates the cache and returns the result. If there is no data in the database, the staff returns a null value. S3: When the redisLevelConfig parameter is not empty and the anti-cache penetration function module is configured in the redisLevelConfig parameter, the Bloom filter solution is used to determine if the key exists, and the next step is performed. Otherwise, null is returned. S4: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache avalanche, data is returned through a method based on the mechanism implemented in the distributed lock solution and the secondary cache (EhCache+Redis) solution; S5: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with a function module for preventing cache breakdown, data is returned through a method based on the mechanism in the distributed lock solution; S6: When the redisLevelConfig parameter is not empty and the redisLevelConfig parameter is configured with an extended function, the extended function is implemented through the new extended function module and the new function data is returned.
3. According to the cache high optimization solution under the microservice of claim 1, Features: The configurable cache update method module adds and updates cache data according to the redisLevelConfig parameter configuration, so that the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown and scalability; The configurable cache query method module can return query results, and through configuration, the cache has the ability to prevent cache penetration, cache avalanche, cache breakdown, and is scalable.
4. According to the cache high optimization solution under the microservice of claim 1, Features: The implementation of the Bloom filter solution is as follows: A1, first define the global Bloom filter; A2, initialize the global Bloom filter and initialize all the data to be queried in the system into the global Bloom filter; A3: When the cache module is configured with the anti-cache penetration function, it will be judged through the Bloom filter. When the key value does not exist in the Bloom filter, it will return directly to block the traffic. When the key value exists, the normal logic will be performed.
5. According to the cache high optimization solution under the microservice of claim 1, Features: The described empty object cache solution caches the returned empty object after a storage layer miss and sets a shorter expiration time.
6. According to the cache high optimization solution under the microservice of claim 1, Features: The distributed lock solution is implemented as follows: B1, first query the cache. If there is data in the cache, the result is returned, indicating that the cache has been set by other threads. If there is no data in the cache, try to obtain the distributed lock; B2, when the lock is acquired successfully, the database is queried, and if there is data in the database, the cache is updated and the result is returned. If there is no data in the database, an empty result is returned; B3, when the lock acquisition fails, it sleeps for a while and then restarts the operation in B1.
7. According to the cache high optimization solution under the microservice of claim 1, Features: The secondary cache (EhCache+Redis) solution includes EhCache cache and Redis cache, wherein the EhCache is used as the first-level cache and the Redis is used as the second-level cache; The implementation method of the secondary cache (EhCache+Redis) solution is: C1, first set up the first-level cache EhCache and the second-level cache Redis; C2, query data, first query the first-level cache, if there is data in the first-level cache, the result is returned, if there is no data, query the second-level cache; C3, query the second-level cache. If there is data in the second-level cache, update the first-level cache and return the result. If the data does not exist, query the database. C4, queries the database. When the data exists, it updates the first-level cache and the second-level cache. When there is no data, it returns empty.
8. According to the cache high optimization solution under the microservice of claim 1, Features: The scheme for evenly distributing the expiration time of Redis keys can avoid the expiration of a large number of keys in a period of time by setting different expiration times for cached keys according to functional modules.
9. According to the cache high optimization solution under the microservice of claim 1, Features: The message middleware solution can be processed by the message middleware to serialize the parallel write; The distributed lock + timestamp solution changes parallel reading and writing into serial reading and writing through distributed locks, and then uses timestamps to ensure that key operations are executed in sequence.
10. According to the cache high optimization solution under the microservice of claim 1, Features: The new extended function module implements the extension of new functions by configuring the dynamic extended attributes and adding new attributes in the redisLevelConfig class.
Citation Information
Patent Citations
Data cache optimizing method and device and computer equipment
CN107977165A
Cache breakdown prevention method and annotation component thereof
CN110764920A
Data cache processing method, device and equipment and cache component
CN110989939A
Data processing method, related equipment and computer readable storage medium
CN112035496A
Data writing method and data reading method based on Bloom filter
CN113536034A
