The performance and optimization of the artifact Redis

The plan is to be finished this week. . .

Analyzing Redis performance mainly considers two issues:

  • 1. Why is Redis so fast ( optimization point\color{red}{optimization point}Optimization of point )
  • 2. How to use Redis faster ( Notes\color{red}{Notes}Note intended to do items )

1. Analyze why Redis is so fast. Let's analyze it from the following aspects:

  • Network layer and operating system layer
  • Memory and data structure
  • Redis itself made those optimizations
  • Ali and other cloud companies have made those optimizations

2. Analyze how Redis can be used faster, we analyze from the following aspects:

  • Redis for caching
  • Prevent cache penetration, cache breakdown, and cache avalanche
  • Separate cold and hot data

Network layer and operating system layer

  • At the network layer, Redis chose a relatively excellent IO model: epoll, which has three more advantages than the previous select model and poll model. For details, see the blog: Three mechanisms of IO multiplexing Select, Poll , Epoll :

    • Optimization point 1: Using the red-black tree structure, firstly, the maximum limit of the handle is gone, and secondly, it is convenient to add, search and delete handles.
    • Optimization point 2: The operation mode is converted from traversal to callback, which greatly improves the IO efficiency.
    • Optimization point 3: Added edge trigger in callback mode, which can be understood as a high priority trigger mode.
    • Optimization point 4: The fd copy problem is copied in full every time it is asked and modified to incremental copy.
  • At the operating system level:

    • Optimization point 5: Redis uses a single thread to avoid resource and time consumption caused by multi-thread switching, but this is not entirely an optimization point. For the entire message flow, the bottleneck is not in the CPU, as if the tap water flow is small (network IO), increasing The person who receives the water (thread) cannot increase the amount of water. In addition, there are two points to note:
      • There is more than one thread in the entire Redis running process, only single thread is used for IO, and multithreading is added in the subsequent 6.0 pages.
      • Even single-threaded Redis can make full use of multi-core CPUs through multiple Redis services, master-slave, read-write separation, and other methods.

Memory and data structure

  • The optimization of the data structure mainly considers the time complexity of data access, which is a way of using space for time:
    • Optimization point 6: Take the jump table as an example: the array is easy to find, which is not conducive to adding, the linked list is good for adding, and it is not conducive to searching, so Redis implements the jump table at the bottom layer, a double-linked list that can be searched in half.
      • Note: In the practice of using a singly linked list to achieve a jump table, it is found that when the data is sorted from small to large, the efficiency is extremely slow to insert from large to small, but if it is a doubly linked list, this problem can be solved well.
  • In memory:
    • Redis memory model
    • Redis memory recycling strategy

Redis itself made those optimizations

Ali and other cloud companies have made those optimizations

Redis for caching

Prevent cache penetration, cache breakdown, and cache avalanche

Separate cold and hot data

【Eggs】

  • Redis monitoring and troubleshooting

Guess you like

Origin blog.csdn.net/ljfirst/article/details/108355193