Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are redis operations on data structures thread safe

How does Redis handle multiple threads (from different clients) updating the same data structure in Redis ? What is the recommended best practice for such a use case?

like image 931
Soumya Simanta Avatar asked Jun 14 '13 00:06

Soumya Simanta


People also ask

Does Redis support multithreading?

The default behavior of redis-benchmark is to achieve throughput by exploiting concurrency only (i.e. it creates several connections to the server). It does not use pipelining or any parallelism at all (one pending query per connection at most, and no multi-threading), if not explicitly enabled via the -P parameter.

Which data structure is thread-safe?

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

How Redis works with single threaded?

A single-threaded program can definitely provide concurrency at the I/O level by using an I/O (de)multiplexing mechanism and an event loop (which is what Redis does). Parallelism has a cost: with the multiple sockets/multiple cores you can find on modern hardware, synchronization between threads is extremely expensive.

Why is single threaded Redis fast?

4 reasons for Redis is single-threadedCPU is not bottleneck : All operations of Redis are memory-based, and CPU is not the bottleneck of Redis. In most cases, the bottleneck of Redis is most likely the size of machine memory or network bandwidth.


1 Answers

if you read the Little redis book at some point this sentence comes.

"You might not know it, but Redis is actually single-threaded, which is how every command is guaranteed to be atomic. While one command is executing, no other command will run."

Have a look in http://openmymind.net/2012/1/23/The-Little-Redis-Book/ for more information

Regards

like image 107
andrefsp Avatar answered Nov 08 '22 08:11

andrefsp