Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is redis multithreaded?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you whether redis is multithreaded, I hope you will gain a lot after reading this article, let's discuss it together!

Redis is a single thread, which means that the network request module uses a single thread (so there is no need to consider concurrency security), that is, one thread handles all network requests, while other modules still use multiple threads.

The reasons why redis can be executed quickly are:

(1) the vast majority of requests are purely memory operations (very fast)

(2) single thread is adopted to avoid unnecessary context switching and competition conditions.

(3) non-blocking IO-IO multiplexing (what does IO multiplexing mean? )

There are three ways of IO multiplexing: select,poll,epoll. It should be noted that select,poll is thread-safe and epoll is thread-safe.

The internal implementation of redis adopts epoll and adopts the simple event framework implemented by epoll+ itself. Read, write, close, and connection in epoll are all converted into events, and then make use of the multiplexing feature of epoll to never waste any time on io. These three conditions are not independent of each other, especially the first, if the request is time-consuming, you can imagine using single-thread throughput and performance. It should be said that redis chose the right technical solution for a particular scenario.

Internal implementation of redis:

The internal implementation adopts epoll and adopts a simple event framework implemented by epoll+ itself. Read, write, close, and connection in epoll are all converted into events, and then make use of the multiplexing feature of epoll to never waste any time on io. These three conditions are not independent of each other, especially the first, if the request is time-consuming, you can imagine using single-thread throughput and performance. It should be said that redis chose the right technical solution for a particular scenario.

Redis on thread safety issues:

Redis actually adopts the concept of thread closure, enclosing tasks in a single thread, which naturally avoids thread safety problems, but for composite operations that rely on multiple redis operations, locks are still required and may be distributed locks.

What are the benefits of using Redis?

(1) it is fast, because the data is stored in memory, and the advantage similar to HashMap,HashMap is that the time complexity of search and operation is O (1).

(2) supports rich data types and string,list,set,sorted set,hash

(3) transactions are supported, and all operations are atomic. the so-called atomicity means that changes to the data are either performed or not performed at all.

(4) rich features: can be used for caching, messages, set expiration time by key, and will be deleted automatically after expiration

Common performance problems and solutions for Redis:

(1) it is best for Master not to do any persistence work, such as RDB memory snapshots and AOF log files; (Master writes memory snapshots, and save command dispatches rdbSave functions, which will block the work of the main thread. When the snapshot is relatively large, it will have a great impact on performance and will intermittently suspend service, so it is best for Master not to write memory snapshots; excessive AOF files will affect the recovery speed of Master restart)

(2) if the data is important, a Slave enables AOF to back up data, and the policy is set to synchronize once per second.

(3) for the speed of master-slave replication and the stability of connection, Master and Slave should be in the same local area network.

(4) try to avoid adding slave libraries to the stressed master libraries.

(5) Master-slave replication does not use graphic structure, but one-way linked list structure is more stable, that is, Master

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report