在实时socket.io node.js应用中,多个Web dynos是否有意义? [英] do multiple web dynos make sense in a real-time socket.io node.js app?

查看:74
本文介绍了在实时socket.io node.js应用中,多个Web dynos是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为heroku上的实时聊天应用程序开发一个node.js后端.当我研究dyno和扩展node.js后端的方法时,我可以看到dynos在http服务器上具有的优势,因为每个dyno可以独立于其他dyno(在大多数情况下都可以).

i'm developing a node.js backend for a real-time chat app on heroku. As i'm looking at dynos and ways to scale the node.js backend, i can see the advantage that dynos could have on http servers because each dyno can be independent from other dynos (which is ok for most cases).

我的问题是:如何扩展和处理实时socket.io应用程序的负载平衡?从我阅读的测功机中可以看到,容器是沙盒装"的:每个测功机都独立于其他测功机运行自己的过程.那么,解决问题的最佳方法是什么?

My question is: how can you scale and handle load balancing of real-time socket.io apps? From what i'm reading dynos are containers that are 'sandboxed': each dyno runs its own process, independent from the other dynos.. so what is the best way to handle the problem?

我当时正在考虑一种解决方案,但它并不优雅或漂亮:

I was thinking of a solution but it isn't elegant or pretty at all:

我可以有多个后台作业,这些作业包含可以在该实例上连接的用户检查新消息的cron..但是我认为必须有一个更好的解决方案.

i could have multiple background jobs that contain crons that can check for new messages for the users connected on that instance.. but i think there must be a better solution.

推荐答案

此问题可以概括为如何使多个节点进程共享数据而无论它们位于同一服务器还是不同服务器上.据我所知,传统的智慧是让所有进程使用一个公共数据库(Postgres,Mongo,Redis)读写数据.只需使用满足您需求的正确数据库即可.

This problem can be generalized to how to get multiple node processes to share data whether they are on the same or different servers. As far as I have seen, the conventional wisdom is to have all of the processes read and write their data using a common database (Postgres, Mongo, Redis). Just use the correct database for your needs.

另一个选择是类似于 MessengerJS 之类的东西,它允许进程间通信.我不知道这对您的应用程序来说是否是个好主意,因为这样您的所有dynos最终都会包含聊天数据的副本.然后由您来确保在dynos之间一切都同步且一致.我将更倾向于将数据库作为事实的唯一来源.

Another option would be something like MessengerJS which allows for interprocess communication. I don't know if this is a good idea for your application because then all of your dynos would end up containing a copy of the chat data. It would then be up to you to make sure everything was synced and consistent between the dynos. I would be more inclined to have a DB as a single source of truth.

这篇关于在实时socket.io node.js应用中,多个Web dynos是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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