扩展 Node.js
我对大规模服务器端开发相当陌生。我想使用 Node.js 编写一个服务器,但在我继续前进之前,我想知道将节点扩展到每秒 20 个查询的一般原则是什么。
我正在编写的服务主要是数据库的接口,以及输入数据的身份验证和验证。
I'm fairly new to large-scale server-side development. I want to write a server using Node.js, but before I forge ahead I'd like to know what the general principles are for scaling node up to, say, 20 queries per second.
The service I'm writing will largely be an interface to a database, plus authentication and validation of input data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
负载平衡
对于最简单的站点来说,很可能根本不需要任何扩展。只需一盒即可满足您的需求。之后,您应该像您提到的那样进行负载平衡,这对于每个架构几乎都是相同的(就像您说您可以首先启动多个节点进程。但是当您变得非常大时,您需要更多的盒子)。
Nginx 负载均衡示例:
Redis
对于 Node.js 来说毫不费力。您应该使用 Redis 作为数据存储,因为它速度非常快:)。当您使用 node_redis 时,甚至还有用于节点的 ac 库。
Hiredis 可以为您提供超强的性能,因为它可以在节点内编译为 C 代码。以下是 redis 与hiredis 一起使用时的一些基准。
当您查看这些数字时,您会发现 20/s 根本不算什么:)。
身份验证
更新:
everyauth
openid
我说了很多,但只是为了爱天哪,请不要尝试实现您自己的身份验证系统。它可能会不安全(很多地方可能会出错),需要大量工作。对于身份验证,您应该使用 facebook-connect、twitter 单点登录等,使用优秀的 connect-auth 图书馆。然后你就安全了,因为他们有专家测试登录系统是否存在漏洞,而且也不会通过纯文本传输密码,但感谢上帝使用 https。我还为想要使用 facebook 连接。
输入数据验证
要验证输入,您可以使用 node-validator。
还有这个 forms 库可以帮助您创建表单。
Load balancing
Most probably for the most simple sites you don't need any scaling at all. Just one single box will get you covered. After that you should do load balancing like you are mentioning which is almost the same for every architecture(like you are saying you could start multiple node processes first. But when you get really big you need more boxes).
Nginx load balancing example:
Redis
No sweat for node.js. You should use redis as your datastore because it is insane fast :). There is even a c library for node when you use node_redis.
Hiredis is what gives you kickass performance because it compiles to C code inside node. Here are some benchmarks from redis when used with hiredis.
When you look at those numbers then 20/s is NOTHING :).
Authentication
Update:
everyauth
openid
I am telling this a lot but for the love of god please don't try to implement your own authentication-system. It is probably going to be unsafe(a lot can go wrong), a lot of work. For authentication you should use facebook-connect, twitter single sign-in, etc using the excellent connect-auth library. Then you are covered safe because they have experts testing there login-systems for holes and the also don't transmit passwords via plain-text but thank for god use https. I also have answered a topic for a user who wanted to use facebook-connect.
validation of input data
To validate input you could use node-validator.
There also is this forms library to help you create forms.