Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking and Redis

We have 75 (and growing) servers that need to share data via Redis. All 75 servers would ideally want to write to two fields in Redis with INCRBYFLOAT operations. We anticipate eventually having potentially millions of daily write operations and billions of daily reads on these two fields. This data must be persistent.

We're concerned that Redis locking might cause write operations to be repeatedly retried with many simultaneous attempts to increment the same field.

Questions:

  • Is multiple, simultaneous INCRBYFLOAT on a single field a bad idea under a very heavy load?
  • Should we have an external process "summarize" separate fields and write the two fields instead? (this introduces another failure point)
  • Will reads on those two fields block while writing?
like image 430
Ovid Avatar asked May 18 '12 09:05

Ovid


People also ask

Can Redis be used for locking?

The simplest way to use Redis to lock a resource is to create a key in an instance. The key is usually created with a limited time to live, using the Redis expires feature, so that eventually it will get released (property 2 in our list). When the client needs to release the resource, it deletes the key.

How does distributed locking work in Redis?

With distributed locking, we have the same sort of acquire, operate, release operations, but instead of having a lock that's only known by threads within the same process, or processes on the same machine, we use a lock that different Redis clients on different machines can acquire and release.

What is Redis used for?

Redis can be used with streaming solutions such as Apache Kafka and Amazon Kinesis as an in-memory data store to ingest, process, and analyze real-time data with sub-millisecond latency. Redis is an ideal choice for real-time analytics use cases such as social media analytics, ad targeting, personalization, and IoT.

What is Redis Redlock?

This is a node. js implementation of the redlock algorithm for distributed redis locks. It provides strong guarantees in both single-redis and multi-redis environments, and provides fault tolerance through use of multiple independent redis instances or clusters.


2 Answers

Since Redis is single threaded, you will probably want to use master-slave replication to separate writes from reads, since yes, writes will generally block reads.

Alternatively you can consider using Apache Zookeeper for this, it provides reliable cluster coordination without single points of failure (like single Redis instance).

like image 109
Ivan Blinkov Avatar answered Nov 08 '22 13:11

Ivan Blinkov


Redis does not lock. Also, it is single threaded; so there are no race conditions. Reads or Writes do not block.

You can run millions of INCRBYFLOAT on the same key without any problems. No need for external processes. Reading those fields does not pose any problems.

That said, "Millions of updates to two keys" sounds strange. If you can explain your use case, perhaps there might be a better way to handle it within Redis.

like image 21
Sripathi Krishnan Avatar answered Nov 08 '22 15:11

Sripathi Krishnan