尽管存在密钥,Redis spop返回null-NodeJS [英] Redis spop returns null despite key being present - NodeJS

查看:116
本文介绍了尽管存在密钥,Redis spop返回null-NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,尽管数据库中存在一个键/条目,但spop Redis命令仍返回null.

I have this issue where the spop Redis command is returning null despite one key/entry being present in the database.

我从未在本地计算机上运行的Redis服务器上遇到此问题.令人讨厌的是,没有错误或任何东西.

I never came across this issue on the Redis server running on my local machine. The annoying thing is that there is no error or anything.

我使用Redis Desktop Manager来检查Heroku Redis数据库中的内容.

I use Redis Desktop Manager to check what is inside the Heroku Redis database.

这是在数据库中获取值的NodeJS代码:

This is the NodeJS code that grabs the value in the database:

redis.spop('id', function (err, result) {}

我可能会做的一件不寻常的事情如下:我同时从数据库中最多处理6个popping值.

One thing that I might do out of the ordinary is as follow: I have up to 6 processes popping values from the database at the same time.

任何想法都可能导致这种奇怪的行为吗?

Any idea what could cause this strange behavior?

  • 本地Disdis:3.0.4
  • Redis远程:Heroku 3.0.3
  • NodeJS模块:ioredis

推荐答案

我的猜测是这样的事情正在发生:

My guess is that something like this is happening:

  • 阅读器1进行检查以查看集合中是否还有数据
  • Redis返回是的,我还有一件商品"
  • 读者#2检查集中是否还有数据
  • Redis返回是的,我还有一件商品"
  • 读者#1弹出此项目(清空集合)
  • 阅读器2尝试弹出该项目,并返回null,因为该集合现在为空.
  • Reader #1 checks to see if there's data left in the set
  • Redis returns "yes, I have one item left"
  • Reader #2 checks to see if there's data left in the set
  • Redis returns "yes, I have one item left"
  • Reader #1 pops this item (emptying the set)
  • Reader #2 tries to pop the item, and gets back a null because the set is now empty.

在检查设置大小和弹出值之间存在竞争状态,这意味着在这两个操作之间有一个很小的时间窗口,另一个读取器也可以弹出值(这就是为什么当您只有一个读者).

There is a race condition between checking the set size and popping a value, which means that there's a small time window in between those two operations that another reader can also pop a value (that's why this issue doesn't happen when you only have one reader).

Redis有一些用于 lists 的命令(例如 BRPOP )等待直到弹出一个实际的项目,但 sets 并没有类似的内容.但是,Redis文档包含有关如何实现的一些示例代码像这样的东西.

Redis has some commands for lists (like BRPOP) that wait until there's an actual item to pop, but there isn't anything similar for sets. However, the Redis documentation contains some example code on how you might be able to implement something like it.

或者,您可以实现某种形式的锁定,尽管这可能会影响性能.

Alternatively, you could implement some form of locking, although that would probably have a performance impact.

最后,如果您的读者从一个空白集中弹出,也许甚至不是什么大问题,在这种情况下,他们只会忽略null并稍后再检查.

In the end, perhaps it's not even a big issue if your readers pop from an empty set, in which case they would just ignore the null and check again later.

这篇关于尽管存在密钥,Redis spop返回null-NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆