Home > Database > Redis > Is redis thread-safe?

Is redis thread-safe?

(*-*)浩
Release: 2019-06-18 10:46:32
Original
7424 people have browsed it

Redis is single-threaded:

Single-threaded refers to the network request module using one thread (so there is no need to consider concurrency safety), that is, one thread handles all network requests. Other modules still use multiple threads.

Is redis thread-safe?

##Thread safety (Recommended learning: Redis video tutorial)

Redis actually adopts the concept of thread closure, which encloses tasks in one thread, which naturally avoids thread safety issues. However, for composite operations that rely on multiple redis operations, locks are still required, and they may be distributed. style lock.

The reasons why redis can execute quickly:

(1) Most requests are pure memory operations (very fast)

(2 ) Uses a single thread to avoid unnecessary context switching and race conditions
(3) Non-blocking IO - IO multiplexing (What does IO multiplexing mean?)

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

The internal implementation of redis uses epoll, using a simple event framework implemented by epoll itself. Read, write, close, and connect in epoll are all converted into events, and then use the multiplexing feature of epoll to never waste any time on io. These three conditions are not independent of each other, especially the first one, if the request All are time-consuming, and the throughput and performance of using a single thread can be imagined. It should be said that redis has chosen the appropriate technical solution for special scenarios.

What are the benefits of using Redis?

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


Rich support Data type, supports string, list, set, sorted set, hash


Supports transactions, operations are all atomic. The so-called atomicity means that all changes to the data are either executed or not executed at all


Rich features: can be used for caching, messaging, setting expiration time by key, it will be automatically deleted after expiration


For more Redis related technical articles, please visit

Redis database usage introductory tutorial column to learn!

The above is the detailed content of Is redis thread-safe?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template