programing

Node.js 확장

nasanasas 2020. 9. 14. 21:16
반응형

Node.js 확장


저는 대규모 서버 측 개발에 상당히 익숙합니다. Node.js를 사용하여 서버를 작성하고 싶지만 계속 진행하기 전에 노드를 초당 20 개의 쿼리까지 확장하는 일반적인 원칙이 무엇인지 알고 싶습니다.

내가 작성하는 서비스는 주로 데이터베이스에 대한 인터페이스와 입력 데이터의 인증 및 유효성 검사입니다.


부하 분산

가장 간단한 사이트의 경우 확장이 전혀 필요하지 않습니다. 단 하나의 상자로 보장됩니다. 그 후에는 모든 아키텍처에 대해 거의 동일한로드 밸런싱을 수행해야합니다 (예 : 먼저 여러 노드 프로세스를 시작할 수 있다고 말한 것처럼 말입니다.하지만 실제로 커지면 더 많은 상자가 필요합니다).

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 를 사용할 때 node에 대한 ac 라이브러리도 있습니다 .

npm install hiredis redis

Hiredis는 노드 내부에서 C 코드로 컴파일되기 때문에 뛰어난 성능을 제공합니다. 다음은 hiredis와 함께 사용할 때 redis의 벤치 마크입니다.

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는 아무것도 아닙니다 :).

입증


최신 정보:


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('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(/^[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('&lt;a&gt;').entityDecode();     //'<a>'

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

참고URL : https://stackoverflow.com/questions/4710420/scaling-node-js

반응형