【发布时间】:2014-08-23 00:16:09
【问题描述】:
好的,已经过去了大约 10 个小时,但我仍然无法弄清楚这一点。有人可以帮忙吗?每次调用我的 Node/Express API 时,我都会写信给 Redis 和 MongoDB。但是,当我用相同的键查询每个数据库时,随着时间的推移,Redis 逐渐开始丢失记录。我可以通过限制整体吞吐量(减少我要求 Redis 执行的操作数)来最小化这种行为。这是伪代码:
function (req, res) {
async.parallel {
f {w:1 into MongoDB -- seems to be working fine}
f {write to Redis -- seems to be miss-firing}
这里是 Redis 代码:
var trx = 1; // transaction is 1:pending 0:complete
async.whilst(function(){return trx;},
function(callback){
r.db.watch(key);
r.db.hgetall(key, function(err, result){
// update existing key
if (result !== null) {
update(key, result, req, function(err, result){
if (err) {callback(err);}
else if (result === null) {callback(null);}
else {trx = 0; callback(null);}
});
}
// new key
else {
newSeries(bin, req, function(err, result){
if (err) {callback(err);}
else if (result === null) {callback(null);}
else {trx = 0; callback(null);}
});
}
});
}, function(err){if(err){callback(err);} else{callback(null);}}
);
在“update”和“newSeries”函数中,我基本上只是使用 HGETALL 中的值对 redis 执行 MULTI/EXEC,并返回结果(以确保我没有遇到竞争条件)。
我正在使用带节点的集群,所以我有多个线程同时执行到 Redis。 任何想法都会非常有帮助。谢谢。
【问题讨论】:
标签: node.js mongodb transactions redis