Advantages and disadvantages of caching and application scenarios

Here we mainly discuss memory – based caching schemes represented by Redis.

Advantages of caching

  • Improve access speed and reduce the time consumption of back-end storage such as database storage
  • Ease back ends like databases

Problems with caching

Every time a component is added to any system, it will inevitably bring additional complexity while bringing new features. It can be said that the system design process is a compromise process. The introduction of caching also raises some issues to consider:

  • Data inconsistency: There is a time window inconsistency between the cache layer and the storage layer. The time window is related to the cache update policy
  • Code maintenance cost: You need to handle the logic of both the cache and storage tiers
  • Operation and maintenance costs: To ensure redis availability and concurrency, will be introducedredis sentinelorredis clusterAnd so on, which increases the complexity of the system and operation and maintenance difficulty.

Application scenarios

  • Expensive and complex operations
  • Accelerated request response

Cache update strategy

  • LRU/LFU/FIFO algorithm
  • Excluding overtime
  • Take the initiative to update

application

  • Suggestion for services with low consistency: Maximum memory + elimination policy
  • High consistency: timeout culling and active updating

The cache to penetrate

Cache penetration: A query for data that does not exist and is not hit by either the cache layer or the storage layer. This causes stress on the storage layer.

Cache penetration findings:

You can usually count them separately in the program

  • The total number of calls
  • Number of cache layer hits
  • Number of hits of the storage tier

If a large number of empty hits are found at the storage tier, a cache penetration problem may have occurred.

Cache penetration solution

  • Caching empty objects
    • Occupied Memory:The reason is thatTo prevent a large number of empty objectsSolution isYou can set a relatively short expiration time, so that it is automatically removed
    • Inconsistent data:The reason is thatThe storage layer adds data, but the cache empty object hasn’t expired yet,Solution isYou can use message queues,
  • Bloomfilter intercept
    • This method is suitable for the application scenarios where the data hit is not high, the data is relatively fixed, the real-time performance is low (usually the data set is large), the code maintenance is complex, but the cache space is small.

Bottomless optimization

Cache clusters hash keys and map them to corresponding nodes. Therefore, key distribution has nothing to do with services. Batch operations are usually obtained from different nodes.

Common I/O optimization ideas:

  • Command optimization, such as SQL statement optimization
  • Reduce network communication times
    • pipeline
    • mget
  • Reduce intervention costs, such as clients using long connection/connection pooling, NIO, etc

Cluster client optimization solution

  • Serial IO, which groups key requests by node and processes them in turn
  • Parallel IO, grouping key requests into nodes and processing them in parallel
  • Hash_tag is implemented to forcibly allocate multiple keys to a node. The operation time of this node is 1 network time + N command time. The operation time of this node is the highest, but the data maintenance cost is high and the data is easily skewed

Optimization of an avalanche

Avalanche definition: The cache layer effectively holds the storage layer because it is carrying a large number of requests, but if the cache layer cannot serve for some reason, then all requests will reach the storage layer and the number of calls to the storage layer will explode.

The bottom line is that the cache is breaking down, putting pressure on the storage tier.

Three aspects of preventing and mitigating the cache avalanche problem

  • Ensure high availability of cache layer services, which Redis providesRedis SentinelandRedis Cluster
  • Rely onisolationThe component is the back endLimit the current and degrade, demotion mechanism, such asHystrix
  • Rehearse in advance. Before the project goes live, rehearse the load of the application and backend and possible problems after the cache layer goes down.

Hotspot key reconstruction was optimized

The cache + expiration time strategy can not only speed up data reading and writing, but also ensure the regular update of data. This mode can basically meet most requirements. But there are two problems:

  • Hot key, very large concurrency
  • Rebuilding the cache cannot be done in a short time (long time to build the cache)

Hot Key Reconstruction

  • The mutex
  • It never expires, but there are inconsistencies during rebuilding