Redis WATCH MULTI EXEC由一个客户提供 [英] Redis WATCH MULTI EXEC by one client

查看:199
本文介绍了Redis WATCH MULTI EXEC由一个客户提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RedisOnGo + node_redis上使用NodeJS + Express + Redis作为客户端。我期待很多并发,所以试图测试WATCH。这个例子不包含Express,只是必要的东西。

I am using NodeJS + Express + Redis on RedisOnGo + node_redis as a client. I expect a lot of concurrency, so trying to test WATCH. This example won't contain Express, just necessary stuff.

var redis = require("redis")
var rc = redis.createClient(config.redis.port, config.redis.host)

rc.auth(config.redis.hash, function(err) {
    if (err) {
        throw err
    }
})

rc.on('ready', function () {
    rc.set("inc",0)
    for(var i=1;i<=10;i++){
        rc.watch("inc")
        rc.get("inc",function(err,data){
            var multi = rc.multi()
            data++ // I do know I can use rc.incr(), this is just for example
            multi.set("inc",data)
            multi.exec(function(err,replies){
                console.log(replies)
            })
        })
    }
})

期待结果:在exec回调中获得N个错误并最终获得inc变量= 10-N。

Expecting result: getting N errors in exec callbacks and finally getting "inc" variable = 10-N.

意外结果:gettin在exec回调中有0个错误,但最终得到inc变量= 1。

Unexpected result: getting 0 errors in exec callbacks but finally getting "inc" variable = 1.

Watch对我的代码不起作用。

Watch doesn't work with my code.

我发现这个帖子 redis和watch + multi允许并发用户。他们说这是因为唯一的redis客户端。

I have found this thread redis and watch + multi allows concurrent users. They say it is because of the only redis client.

然后我发现这个帖子我应该为每个连接创建一个新的Redis客户端吗?。他们说为每笔交易生成一个新客户绝对不推荐。我迷路了。

Then I found this thread Should I create a new Redis client for each connection?. They say that generating a new client for each transaction "is definitely not recommended". I am lost.

请注意,我必须向Redis服务器进行身份验证。提前致谢!

Please also note, that I have to authenticate to Redis server. Thanks in advance!

第1版:

我能够做到通过在每次WATCH-MULTI-EXEC迭代之前创建新的客户端连接,使用本地Redis实例(因此我不使用client.auth)。不确定它是否合适,但结果现在100%准确。

I was able to make it work using local Redis instance (so I do not use client.auth) by creating a new client connection before each WATCH-MULTI-EXEC iteration. Not sure if it is good though, but results now are 100% accurate.

第2版
如果我创建它在每次WATCH-MULTI-EXEC迭代之前建立新的客户端连接,然后执行client.auth并等待client.on。

EDITION 2 Made it work if I create a new client connection before each WATCH-MULTI-EXEC iteration and then do client.auth and wait for client.on.

问题仍然存在,我可以为每次迭代创建新的客户端连接?

The question still exists, is it OK that I create new client connections for each iteration?

推荐答案

您的结果完全可以预测。这是正确的。

Your result is entirely predictable. And rightly so.

请记住 - node.js是一个线程应用程序。 Node.js使用异步输入输出,但命令应该在redis中严格按顺序发送请求 - 响应。因此,当您只使用一个连接到redis服务器时,您的代码和请求将严格并行执行。

Keep in mind - node.js is one thread application. Node.js use asynchronous input-output, but the commands should be sent in redis strictly sequential "request-response". So your code and your requests executed strictly parallel while your are using just one connection to redis server.

查看代码:

rc.on('ready', function () {
    rc.set("inc",0)
    for(var i = 1; i <= 10; i++){
        rc.watch("inc")
        //10 times row by row call get function. It`s realy means that your written
        //in an asynchronous style code executed strict in series. You are using just
        //one connection - so all command would be executed one by one.
        rc.get("inc",function(err,data){
            //Your data variable data = 0 for each if request.
            var multi = rc.multi()
            data++ //This operation is not atomic for redis so your always has data = 1
            multi.set("inc",data) //and set it
            multi.exec(function(err,replies){
                console.log(replies) 
            })
        })
    }
})

要确认这一步骤:


  1. 连接到redis并执行 monitor 命令。

  2. 运行你的node.js应用程序

输出将

    SET inc 0
    WATCH inc

    GET inc 
    .... get command more 9 times

    MULTI
    SET inc 1
    EXEC
    .... command block more 9 times

这样你就可以得到你上面写的结果:在exec回调中得到0个错误,但最终得到inc变量= 1。 。

So that you get exactly the results that you wrote above: "getting 0 errors in exec callbacks but finally getting "inc" variable = 1.".

您是否可以为每次迭代创建新的客户端连接?

对于这个样本 - 是的,它解决了你的问题。通常 - 它取决于您要运行多少并发查询。 Redis仍然是一个线程,所以这个并发意味着只需要并发命令批处理到redis引擎。

For this sample - yes, its solves your problem. In general - it depends on how many "concurrent" query you want to run. Redis is still one threaded so this "concurrent" means just way to concurrent command batch to redis engine.

例如,如果使用2个连接,监视器可能会给出如下内容:

For example, if use 2 connections the monitor could give something like this:

 1 SET inc 0 //from 1st connection
 2 WATCH inc //from 1st connection
 3 SET inc 0 //from 2nd connection            
 4 GET inc //from 1nd connection            
 5 WATCH int //from 2nd connection       
 6 GET inc //from 2nd connection                 
 7 MULTI //from 1st connection           
 8 SET inc 1 //from 1st connection    
 9 MULTI //from 2nd connection           
10 SET inc 1 //from 2nd connection           
11 EXEC //from 1st failed becouse of 2nd connection SET inc 0 (line 3) 
        //was executed after WATCH (line 2) 
12 EXEC //success becouse of MULTI from 1st connection was failed and SET inc 1 from first 
        //connection was not executed

-------------------------------------------------------------------------------> time 
               |   |    |  |   |     |     |    |   |     |    |         |
connection 1  set watch | get  |     |   multi set  |     |   exec(fail) |
connection 2          set    watch  get            multi set            exec

理解redis如何执行你的非常重要命令。 Redis是单线程的,所有连接中的所有命令都是逐行执行的。 Redis不保证来自一个连接的命令将连续执行(如果此处存在另一个连接),所以如果要确保您的命令执行一个块(如果需要),则应该是MULTI。但为什么需要WA​​TCH呢?看看我上面的redis命令。您可以看到来自不同连接的命令是混合的。并且手表允许您管理这个。

Its very important to understand how redis execute your commands. Redis is single threaded, all command from all connection executed one-by-one in a row. Redis does not guarantee that command from one connection would be executed in a row (if here is another connections present) so your should MULTI if want be sure that your commands executed one block (if need it). But why WATCH needed? Look at my redis commands above. You can see that command coming from different connections are mixed. And watch allow you to manage this.

文档中对此进行了详细解释。请阅读!

This beautifully explained in the documentation. Please read it!

这篇关于Redis WATCH MULTI EXEC由一个客户提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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