1

Can XREAD (or perhaps another command) be used to atomically detect whether data was written to a Redis stream?

More specifically:

Suppose you added some data to a Redis stream in one process and saw that the data was added successfully with some auto generated key.

XADD somestream foo bar

After this XADD completes, you immediately run the following read in another process.

XREAD COUNT 1000 STREAMS somestream 0-0

Is this XREAD guaranteed to return data? The documentation is not clear about whether a successful XADD guarantees that readers will immediately see the added data, or whether there might be some small delay.

Max
  • 1,670
  • 1
  • 12
  • 17

1 Answers1

1

Redis's famous single threaded architecture answers that question. When you execute XADD on one process(client side) and after another process(client side) executes XREAD then the server execute them consecutively which guarantees that the data will be there before XREAD is executed.

The next quotes are from The Little Redis Book

Every Redis command is atomic, including the ones that do multiple things. Additionally, Redis has support for transactions when using multiple commands.

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. (We’ll briefly talk about scaling in a later chapter.) This is particularly useful when you consider that some commands do multiple things.

Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • Is there a stronger guarantee also available then that if I `XREAD COUNT 1000` then I will deterministically get 1000 elements if there are 1000 elements available to be read? – Max Jun 25 '20 at 20:12
  • @Max Exactly. Same applies to other read operations on different data structures too. – Ersoy Jun 25 '20 at 20:17
  • @Max one small detail, if you have more one instance(multiple replica/sentinel etc) etc, then the reads may not be consistent - but that's totally different case. You may google CAP theorem for this, it is a big area to mention in the comments. – Ersoy Jun 25 '20 at 20:21
  • Thanks. In this case, there is a replica hanging off a primary instance, but both XREAD and XADD are happening on the primary instance. Do you know if these operations are guaranteed to be atomic in this case? Given what you've said I suspect the answer is yes, but maybe there is some delay related to syncing the replica? – Max Jun 25 '20 at 20:53
  • @Max Yes that is the nature of distributed systems, you can't ignore network or partition tolerance when you have a cluster more than one instance. You either have strong consistency(wait all replicas to have written data) or eventual consistency(don't wait all of them to have it)/high availability . It happens in all distributed systems. You give up on either consistency or availability. Redis documentation explains it well how redis handles it. – Ersoy Jun 25 '20 at 20:56