티스토리 뷰

안녕하세요.

오늘은 PM2의 Cluster mode에 대해서 알아보겠습니다.


먼저 PM2가 무엇일까요?

PM2는 Node.js 애플리케이션의 프로세스 관리를 위해 사용하며, 아래와 같은 이점을 얻을 수 있습니다.

 

  • 프로그램의 예상치 못한 종료 시 자동 재시작
  • 무중단 서비스
  • Cluster mode
  • Monitoring

왜 갑자기 Cluster mode가 궁금했을까요?

저는 PM2의 기능 중에서 Cluster mode와 Monitoring 기능이 참 매력적이라고 생각해왔습니다.

그러던 어느 날 문득 이런 생각을 했습니다.

 

1. Cluster mode를 켜면 성능이 정말 좋네.
2. 여러 개를 띄우니까 그런가..?
3. 엥.. Node.js 프로세스 여러 개를 띄우면 Port 사용이 겹칠 텐데
4. 그럼 뭐지..?

여러분도 비슷한 생각을 하신 적이 있으신가요?

대체 Cluster mode가 어떻게 작동할까요?


PM2 - Cluster mode

cluster mode

PM2 - keymetrics의 Cluster mode에 대한 설명은 다음과 같습니다.

 

The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled accross all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available.
Under the hood, this uses the Node.js cluster module such that the scaled application’s child processes can automatically share server ports.

즉 Cluster mode는

 

  • 코드 수정 없이 Node.js 애플리케이션을 CPU에 따라 확장할 수 있습니다.
  • 안정성과 성능을 크게 향상할 수 있습니다.
  • Node.js의 Cluster module을 통해 동작합니다.

결국 우리가 최종적으로 알아봐야 할 것Node.js의 Cluster module이 되겠습니다.


Node.js - Cluster module

Node.js 공식 홈페이지의 Cluster module에 대한 설명은 다음과 같습니다.

 

A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows easy creation of child processes that all share server ports.

Node.js의 단일 인스턴스는 단일 스레드에서 실행되는데, 멀티코어 시스템을 이용하기 위해서 Node.js 프로세스들을 클러스터로 사용할 수 있습니다. 이를 통해 모든 서버 port를 공유하는 하위 프로세스를 생성합니다.

 

 

The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.

이 하위 프로세스들은 child_process.fork() 메서드를 사용해서 생성되는데, 부모 자식 간의 통신을 위한 IPC(Inter-process communication) 채널을 가지고 있으며 생성된 각 프로세스는 자체 V8인스턴스가 있습니다.


정리

  1. PM2 - Cluster mode는 Node.js - Cluster module을 통해 작동합니다.
  2. Node.js Cluster module은 Node.js에서 멀티코어 시스템을 이용할 수 있도록 Port를 공유하는 Child process를 생성합니다.
  3. 요청을 Parent process가 받은 후 Child process들에 재분배합니다.
  4. 각 Child process는 각각의 V8에서 작동하므로, Node 인스턴스를 더 띄운 것과 같이 동작합니다.
  5. 인메모리에 의존하지 않는 프로그램을 만들면 위와 같은 확장에 더 용이합니다.

'Node.js' 카테고리의 다른 글

setTimeout(), setImmediate(), process.nextTick()  (2) 2020.01.19
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함