Scaling node.js

I am new to large-scale server-side development. I want to write a server using node.js, but before I move forward, I would like to know what are the general principles for scaling a node to, say, 20 requests per second.

The service I'm writing will basically be the interface to the database, and also validates and validates the input.

+84
scalability
Jan 17 2018-11-11T00:
source share
1 answer

Load balancing

Most likely, for the simplest sites you do not need to scale at all. Only one box will help you reach you. After that, you should do load balancing, as you mention, which is almost the same for each architecture (for example, you say that you can run several node processes, but when you get really large, 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 requests per second

No sweat for node.js. You should use redis as your data store because it is insanely 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 tests 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 these numbers, then 20 / s NOTHING :).

Authentication




Update:




I say this a lot, but for the love of God, please do not try to implement your own authentication system. It will probably be unsafe (a lot can go wrong), a lot of work. For authentication, you must use facebook-connect, twitter single sign-in, etc. Using the excellent connect-auth library. Then you are protected from security because they have experts who test login systems for holes, and also do not pass passwords through text text, but thank you for using https for god. I also answered the topic for a user who wanted to use facebook-connect .

input validation

To check input, you can use node-validator .

 var check = require('validator').check, sanitize = require('validator').sanitize //Validate check('test@email.com').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(/^[az]+$/); //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('&lt;a&gt;').entityDecode(); //'<a>' 

There is also a forms library that helps you create forms.

+143
Jan 17
source share



All Articles