Redis 和 Node.js 以及 Socket.io 问题 [英] Redis and Node.js and Socket.io Questions

查看:67
本文介绍了Redis 和 Node.js 以及 Socket.io 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在学习 redis 和 node.js 有两个问题我找不到任何令人满意的答案.

I have been just learning redis and node.js There are two questions I have for which I couldn't find any satisfying answer.

我的第一个问题是关于在 node.js 中重用 redis 客户端.我找到了这个问题和答案:如何在套接字中重用 redis 连接.io? ,但还不够让我满意.

My first question is about reusing redis clients within the node.js. I have found this question and answer: How to reuse redis connection in socket.io? , but it didn't satisfy me enough.

现在,如果我在连接事件中创建 redis 客户端,它将为每个连接生成.因此,如果我有 20k 个并发用户,那么将有 20k 个 redis 客户端.

Now, if I create the redis client within the connection event, it will be spawned for each connection. So, if I have 20k concurrent users, there will be 20k redis clients.

如果我把它放在连接事件之外,它只会产生一次.

If I put it outside of the connection event, it will be spawned only once.

答案是说他在连接事件之外为每个函数创建了三个客户端.

The answer is saying that he creates three clients for each function, outside of the connection event.

但是,根据我对 MySQL 的了解,在编写生成子进程并并行运行的应用程序时,您需要在创建子实例的函数中创建 MySQL 客户端.如果在它之外创建它,MySQL 将给出MySQL 服务器已消失"的错误,因为子进程将尝试使用相同的连接.应该为每个子进程分别创建.

However, from what I know MySQL that when writing an application which spawns child processes and runs in parallel, you need to create your MySQL client within the function in which you are creating child instances. If you create it outside of it, MySQL will give an error of "MySQL server has gone away" as child processes will try to use the same connection. It should be created for each child processes separately.

所以,即使你为每个函数创建三个不同的 redis 客户端,如果你有 30k 并发用户并发发送 2k 消息,你应该会遇到同样的问题,对吧?因此,每个用户"都应该在连接事件中拥有自己的 redis 客户端.我对吗?如果没有,node.js 或 redis 如何处理并发请求,与 MySQL 不同?如果它有自己的机制并在 redis 客户端中创建类似子进程的东西,那为什么我们需要创建三个不同的 redis 客户端呢?一个就够了.

So, even if you create three different redis clients for each function, if you have 30k concurrent users who send 2k messages concurrently, you should run into the same problem, right? So, every "user" should have their own redis client within the connection event. Am I right? If not, how node.js or redis handles concurrent requests, differently than MySQL? If it has its own mechanism and creates something like child processes within the redis client, why we need to create three different redis clients then? One should be enough.

我希望问题很清楚.

-- 更新 --

我找到了以下问题的答案.http://howtonode.org/control-flow无需回答,但我的第一个问题仍然有效.

I have found an answer for the following question. http://howtonode.org/control-flow No need to answer but my first question is still valid.

-- 更新 --

我的第二个问题是这个.我也不擅长 JS 和 Node.js.所以,据我所知,如果你需要等待一个事件,你需要将第二个函数封装在第一个函数中.(我还不知道术语).举个例子吧;

My second question is this. I am also not that good at JS and Node.js. So, from what I know, if you need to wait for an event, you need to encapsulate the second function within the first function. (I don't know the terminology yet). Let me give an example;

socket.on('startGame', function() {
    getUser();
    socket.get('game', function (gameErr, gameId) {
        socket.get('channel', function (channelErr, channel) {
            console.log(user);
            client.get('games:' + channel + '::' + gameId + ':owner', function (err, owner) { //games:channel.32:game.14
                if(owner === user.uid) {
   //do something
                }
            });
        }
    });
});

因此,如果我正确地学习它,如果我需要等待 I/O 响应,我需要运行函数中的每个函数.否则,node.js 的非阻塞机制将允许第一个函数运行,在这种情况下它会并行获取结果,但如果需要时间来获取第二个函数可能没有结果.因此,例如,如果您从 redis 获取结果,并且将在第二个函数中使用该结果,则必须将其封装在 redis get 函数中.否则第二个函数将运行而不会得到结果.

So, if I am learning it correctly, I need to run every function within the function if I need to wait I/O answer. Otherwise, node.js's non-blocking mechanism will allow the first function to run, in this case it will get the result in parallel, but the second function might not have the result if it takes time to get. So, if you are getting a result from redis for example, and you will use the result within the second function, you have to encapsulate it within the redis get function. Otherwise second function will run without getting the result.

那么,在这种情况下,如果我需要运行 7 个不同的函数,而 8. 函数将需要所有这些函数的结果,我是否需要像这样递归地编写它们?或者我错过了什么.

So, in this case, if I need to run 7 different functions and the 8. function will need the result of all of them, do I need to write them like this, recursively? Or am I missing something.

我希望这也很清楚.

非常感谢,

推荐答案

因此,每个用户"都应该在连接事件中拥有自己的 redis 客户端.我说得对吗?

So, every "user" should have their own redis client within the connection event. Am I right?

其实你不是:)

问题是 node.js 与 PHP 非常不同.node.js 不会在新连接上产生子进程,这是它可以轻松处理大量并发连接的主要原因之一,包括长连接(Comet、Websockets 等).node.js 使用单个进程中的事件队列按顺序处理事件.如果您想使用多个进程来利用多核服务器或多台服务器的优势,则必须手动进行(不过如何操作超出了本问题的范围).

The thing is that node.js is very unlike, for example, PHP. node.js does not spawn child processes on new connections, which is one of the main reasons it can easily handle large amounts of concurrent connections, including long-lived connections (Comet, Websockets, etc.). node.js processes events sequentially using an event queue within one single process. If you want to use several processes to take advantage of multi-core servers or multiple servers, you will have to do it manually (how to do so is beyond the scope of this question, though).

因此,使用单个 Redis(或 MySQL)连接来为大量客户端提供服务是一种非常有效的策略.这避免了为每个客户端请求实例化和终止数据库连接的开销.

Therefore, it is a perfectly valid strategy to use one single Redis (or MySQL) connection to serve a large quantity of clients. This avoids the overhead of instantiating and terminating a database connection for each client request.

这篇关于Redis 和 Node.js 以及 Socket.io 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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