This is my third article about getting started
A, introducing
Caching is often the easiest and most effective way to improve system performance.
The purpose of caching is to bring data closer to the user and make it accessible faster.
Caching is mainly used in two scenarios:
- Read more data than write
- Query data that takes a long time and requires complex processing
Frequently read data, frequently accessed data, hot data, I/O bottleneck data, computationally expensive data, unchanging data, weak consistency data
Cached data generally consists of a Key and a Value.
Key design considerations:
- No collision: Ensure that two different data have different keys to avoid incorrect cache hits
- Efficient generation: Given data, the corresponding Key is generated at a low cost
- Efficient comparison: Given two keys, you need to quickly compare them to see if they are consistent
Common cache classification
1. Client cache (browser)
The user is the most initial request initiator, and the module that interacts with the user is called the client (such as browser, computer client, mobile phone, embedded system interface, etc.).
For example, when we use the browser to access some web pages or Http access, we can set the response header according to the cache returned by the server to cache the response content in the browser. Next time, we can directly use the cache content or just go to the server to verify whether the cache is expired.
2. Gateway/Reverse proxy
Static resources such as images, videos and music from the home page can be hosted on a Web server such as Nginx.
This is where you can cache user-independent elements, pages, and other general-purpose data.
3. Local cache
A cache component in an application. The application and cache are stored in the same process without too much network overhead.
This method is applicable to single-node applications and caches data with a small amount, small changes, and high access frequency.
Common local Cache components include HashMap, Guava Cache, and Caffeine.
4. Distributed cache
Common distributed caches include Redis, Memcached, and Tair. Among them, the data structure provided by Redis is rich and easy to use, so it is most widely used in application practice.
4. Database cache
For example, Mysql Query Cache and SQL Server Procedure Cache.
Cache update strategy
1. Cache-Aside
- Read: Reads data from the cache. If the data exists, it is directly returned. If the data does not exist, it is fetched from the database and written to the cache
- Write: Writes data to the database and invalidates the corresponding cache
This pattern maintains the cache directly by the business code.
2. Cache-As-SoR
SoR(System of Record): A System that stores original data.
In this mode, all operations are performed on the Cache, and the Cache entrusts THE SoR to complete data read and write operations. That is, business code can only interact with the Cache, and the Cache interacts with the database for data reading and writing.
2.1 the Read/Write Through
- Read: Reads data from the cache. If the data exists, it is directly returned. If the data does not exist, it is read from the database, written to the Cache, and returned by a caching service (such as Guava Cache, where the source data is queried by the CacheLoader).
- Write: The cache service is invoked to write data to the cache and database at the same time
2.2 Write Behind
This mode is called Write Back mode, also known as Write Back.
Unlike Write Through, which writes to the cache and database synchronously, Write Back writes to the cache first and then asynchronously to the database. After asynchronous, batch write, merge write, delay, and traffic limiting can be implemented.
In daily practical applications, cache-aside mode is relatively common. In this pattern, caching is used to speed up queries and can be used as an auxiliary; When the cache is faulty, you can directly query the database to complete read and write operations. In cache-AS-SOR mode, the caller can only call the Cache service directly, and the real raw data system is not visible to it. If the cache service fails, data cannot be read or written and related functions are affected.
4. Cache clearing strategy
1. Time-sensitive cleaning
Set the expiration time for cached data, and automatically clear the data after the expiration time.
The time-dependent cleanup strategy is easy to use and limits the lifetime of data, but does not limit the size of the cache.
2. Number threshold clearing
Number threshold cleaning can indirectly limit the size of cache space by limiting the number of data. When the data in the cache reaches a certain threshold, the cache is cleaned according to different policies.
2.1. FIFO
First in First out (First in First out) : When the cache reaches the data limit, the First data written to the cache is cleaned First
2.2 the LRU
Least Recently Used. If a piece of data has been accessed Recently, it is considered likely to be accessed again. If a piece of data hasn’t been accessed for a long time, it probably won’t be accessed again.
Therefore, when the cache is cleared, the data that has not been accessed for a long time is cleared first, and the recently accessed data is retained.
2.3 LFU
The Least Frequently Used data is preferred over the data that is accessed Least Frequently
5. Cache risk
1. Cache penetration
Concept: The caller queries data that does not exist in the cache, causing the cache to fail to hit and penetrate the backend database for query. When such requests occur at high frequency and in large numbers, they place a great strain on the system.
** Solution:
-
Returns a null value or business default value and caches it.
-
Bloom filter: Checks whether the data exists through the Bloom filter. If the data does not exist, a null value is returned, and no further query is required in the cache and database.
Bloom filters can be used to determine whether an element is in a collection.
A. Use n hash functions to calculate n hash values for a certain data
B. Modulo n hash values to the length of bit data to obtain the corresponding position of each hash value in the array
C. Set the corresponding bit to 1
-
Add pre – related verification, avoid malicious attacks and crawlers, disable illegal requests
2. Cache avalanche
Concept: Refers to the sudden failure of a large number of caches, causing great pressure on back-end services and databases.
Solution:
- Use different expiration times for different data
- Use different expiration times for the same type of data (fixed duration + random duration)
3. Cache breakdown
Concept: A frequently accessed hot Key suddenly fails, and a large number of requests accessing the database at the same time causes great pressure.
Solution:
- Lock (standalone, distributed) : Only one request can access the database and update the cache at a time.
- Set hotspot data to never expire and background update: Set the data validity period to permanent and periodically update data in the background to prevent data from being cleared due to insufficient memory
- If this article is useful to you, just give it a thumbs up and leave!
- If you have any questions, please leave a comment!
- Welcome to reprint, please indicate the source!