I just studied redis and node.js. There are two questions that I have for which I could not find a satisfactory answer.
My first question is reusing redis clients in node.js. I found this question and answer: How to reuse a redis connection in socket.io? but that did not satisfy me enough.
Now, if I create a redis client in a connection event, it will be created for each connection. So, if I have 20k concurrent users, there will be 20,000 redis clients.
If I put it outside the connection event, it will be created only once.
The response says that it creates three clients for each function, outside the connection event.
However, from what I know MySQL, when writing an application that spawns child processes and runs in parallel, you need to create your MySQL client as part of the function in which you create child instances. If you create it outside of it, MySQL will give the error "MySQL server is gone" because child processes will try to use the same connection. It must be created for each child process separately.
That way, even if you create three different redis clients for each function, if you have 30k concurrent users who send 2k messages at the same time, you should run into the same problem, right? Thus, each "user" must have its own redis client in the connection event. I'm right? If not, how does node.js or redis handle concurrent queries other than MySQL? If it has its own mechanism and creates something like child processes in the redis client, why do we need to create three different redis clients? You need to be enough.
Hope this question was clear.
- UPDATE -
I found the answer to the following question. http://howtonode.org/control-flow No need to answer, but my first question is still valid.
- UPDATE -
My second question. I am also not so 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 in the first function. (I do not 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) {
So, if I study it correctly, I need to run each function inside the function, if I need to wait for an I / O response. Otherwise, the node.js locking mechanism will allow the first function to work, in this case it will get the result in parallel, but the second function may not have the result if it takes time to get it. So, if you get the result from redis, for example, and you use the result in the second function, you must encapsulate it in the redis get function. Otherwise, the second function will work without receiving a result.
So, in this case, if I need to run 7 different functions, and the 8th function needs the result of all of them, do I need to write them this way, recursively? Or am I missing something.
Hope this was clear too.
Many thanks,