扩展 Node.js

发布于 2024-10-12 05:29:58 字数 124 浏览 0 评论 0原文

我对大规模服务器端开发相当陌生。我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-10-19 05:29:58

负载平衡

对于最简单的站点来说,很可能根本不需要任何扩展。只需一盒即可满足您的需求。之后,您应该像您提到的那样进行负载平衡,这对于每个架构几乎都是相同的(就像您说您可以首先启动多个节点进程。但是当您变得非常大时,您需要更多的盒子)。

Nginx 负载均衡示例

http {
  upstream myproject {
    server 127.0.0.1:8000 weight=3;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;    
    server 127.0.0.1:8003;
  }

  server {
    listen 80;
    server_name www.domain.com;
    location / {
      proxy_pass http://myproject;
    }
  }
}

Redis

每秒 20 次查询

对于 Node.js 来说毫不费力。您应该使用 Redis 作为数据存储,因为它速度非常快:)。当您使用 node_redis 时,甚至还有用于节点的 ac 库。

npm install hiredis redis

Hiredis 可以为您提供超强的性能,因为它可以在节点内编译为 C 代码。以下是 redis 与hiredis 一起使用时的一些基准。

PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287

当您查看这些数字时,您会发现 20/s 根本不算什么:)。

身份验证


更新:


我说了很多,但只是为了爱天哪,请不要尝试实现您自己的身份验证系统。它可能会不安全(很多地方可能会出错),需要大量工作。对于身份验证,您应该使用 facebook-connect、twitter 单点登录等,使用优秀的 connect-auth 图书馆。然后你就安全了,因为他们有专家测试登录系统是否存在漏洞,而且也不会通过纯文本传输密码,但感谢上帝使用 https。我还为想要使用 facebook 连接

输入数据验证

要验证输入,您可以使用 node-validator

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('[email protected]').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode();     //'<a>'

还有这个 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:

http {
  upstream myproject {
    server 127.0.0.1:8000 weight=3;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;    
    server 127.0.0.1:8003;
  }

  server {
    listen 80;
    server_name www.domain.com;
    location / {
      proxy_pass http://myproject;
    }
  }
}

Redis

20 queries per second

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.

npm install hiredis 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.

PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287

When you look at those numbers then 20/s is NOTHING :).

Authentication


Update:


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.

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('[email protected]').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode();     //'<a>'

There also is this forms library to help you create forms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文