Redis – 处理并发读写

huangapple go评论67阅读模式
英文:

Redis - Handle Concurrent Reads And Writes

问题

我正在使用 Redis 数据库,经常需要读取大量数据。然而,在我进行读取的过程中,可能会有另一个进程同时写入相同的数据库,可能会改变我正在尝试读取的数据。

这对我造成了问题,因为当我发起读操作时,我需要数据快照在读取开始时是一致的,不受任何并发写操作的影响。

所以,我的问题是:

  1. 是否有一种方法可以配置读取过程,以检索数据状态,就像在读操作开始时一样,而不管在读取过程中可能发生的任何并发写入?
  2. 如果没有,Redis 中是否有一种机制可以在进行读取时暂时阻止写入操作?

我尝试过使用事务来进行读取和写入,但我注意到我读取的数据仍然会被写入操作改变。
我也考虑过使用 redlock,但它不符合我的需求,因为我希望允许并发读取。

英文:

I'm working with a Redis database where I often have to read a large set of data. However, while I'm in the process of reading, there could be another process concurrently writing to the same database, potentially altering the data that I'm trying to read.

This creates a problem for me, because when I initiate a read operation, I need the data snapshot to be consistent as of the moment the read starts, irrespective of any concurrent write operations.

So, my questions are:

  1. Is there a way to configure the reading process to retrieve the state of the data as it was at the start of the read operation, despite any concurrent writes that may happen during the read?
  2. If not, is there a mechanism in Redis that can temporarily block write operations while a read is underway?

I’ve tried using transactions both for reading and writing but I noticed that the data I read is still being changed by the writing operation.
I also thought about using redlock but it doesn’t fit my needs because I want to allow concurrent reading.

答案1

得分: 0

Redis事务终究是我的解决方案。
Redis事务在读取时会“阻塞”其他操作。

正如官方Redis事务文档所述:

> 事务中的所有命令都会被串行化并按顺序执行。另一个客户端发送的请求永远不会在执行Redis事务的过程中得到服务。这确保了命令作为单个隔离操作执行。

英文:

Redis Transactions was the solution for me after all.
Redis transactions do "block" other operations while reading.

As the official redis Transactions docs says:

> All the commands in a transaction are serialized and executed sequentially. A request sent by another client will never be served in the middle of the execution of a Redis Transaction. This guarantees that the commands are executed as a single isolated operation.

huangapple
  • 本文由 发表于 2023年7月27日 20:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定