开发者

Node.js tcp socket server on multiple machines

开发者 https://www.devze.com 2023-04-07 17:47 出处:网络
I have a node.js tcp server that is used as a backend to an iPhone chat client. Since my implementation includes private group chats I sto开发者_StackOverflow社区re a list of users and what chat room

I have a node.js tcp server that is used as a backend to an iPhone chat client. Since my implementation includes private group chats I sto开发者_StackOverflow社区re a list of users and what chat room they belong to in memory in order to route messages appropriately. This all works for fine assuming my chat server will always be on one machine, but when/if I need to scale horizontally I need a good way of broadcasting messages to clients that connect to different servers. I don't want to start doing inter-process communication between node servers and would prefer sharing state with redis.

I have a few ideas but I'm wondering if anyone has a good solution for this? To be clear here is an example:

User 1 connects to server 1 on room X, user 2 connects to server 2 on room X. User 1 sends a message, I need this to be passed to user 2, but since I am using an in memory data structure the servers don't share state. I want my node servers to remain as dumb as possible so I can just add/remove to the needs of my system.

Thanks :)


You could use a messaging layer (using something like pub/sub) that spans the processes:

                             Message Queue
-------------------------------------------------------------------------------
            |                                     |
         ServerA                               ServerB
         -------                               -------
Room 1: User1, User2                  Room 1: User3, User5
Room 2: User4, User7, User11          Room 2: User6, User8
Room 3: User9, User13                 Room 3: User10, User12, User14

Let's say User1 sends a chat message. ServerA sends a message on the message queue that says "User1 in Room 1 said something" (along with whatever they said). Each of your other server processes listens for such events, so, in this example, ServerB will see that it needs to distribute the message from User1 to all users in its own Room 1. You can scale to many processes in this way--each new process just needs to make sure they listen to appropriate messages on the queue.

Redis has pub/sub functionality that you may be able to use for this if you're already using Redis. Additionaly, there are other third-party tools for this kind of thing, like ZeroMQ; see also this question.


Redis is supposed to have built in cluster support in the near future, in the mean time you can use a consistent hashing algorithm to distribute your keys evenly across multiple servers. Someone out there has a hashing module for node.js, which was written specifically to implement consistent hashing for a redis cluster module for node.js. You might want to key off the 'room' name to ensure that all data points for a room wind up on the same host. With this type of setup all the logic for which server to use remains on the client, so your redis cluster can basically remain the same and you can easily add or remove hosts.

Update

I found the consistent hashing implementation for redis I was talking about, it gives the code of course, and also explains sharding in an easy to digest way.

http://ngchi.wordpress.com/2010/08/23/towards-auto-sharding-in-your-node-js-app/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号