78
Practices for designing scalable architecture base on // 2015.11.12

Play node conference

  • Upload
    john-kim

  • View
    13.623

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Play node conference

Practices for designing scalable architecture base on

// 2015.11.12

Page 2: Play node conference

김요한 [email protected] Architect

Page 3: Play node conference
Page 4: Play node conference

Node.js 기반의 대용량 분산 서버 아키텍처 설계 사례

1. 비동기 병렬처리 사례“ PhantomJS 기반의 이미지 최적화 처리 시스템 ” : AWS, MQ 기반의 비동기 처리

2. 실시간 메시지 처리 사례“ socket.io 기반의 실시간 채팅 시스템 ” : zookeeper, redis 활용 : 효율적인 Weighted Consistent Hashing 구현 : 프로세스 모니터링 & 실시간 서버 모니터링

Page 5: Play node conference

CPU In

tensiv

e Task

http://www.slideshare.net/Byungwook/nodejs-vertx

Page 6: Play node conference

PayPal wrote the same app twice in Node and Java Node was  2x requests/sec,  33% less code,  35% less response time.

코드 라인 수를 줄일 수 있다.간단하다. 빨리 개발할 수 있다.

Page 7: Play node conference

Ruby on Rails is like a minivan.  It has all the features. Node is like a Ferrari.

페라리 만큼 빠르다.CPU 타입이나, OS 가 무엇인지, 그리고 어떤 걸 개발하느냐에 따라 다르다. 개발 속도 만큼은 정말 빠르다. 난 미니벤도 좋은데 . . .

Page 8: Play node conference

Fast deployment is a huge advantage for node.  The 250-500 ms node process start / stop time has been a huge benefit. Hard to deploy multiple times a day when it takes 2 hours each time.

서버 시작 / 중지 시간이 상당히 빠르다.배포시간이 짧다. 하루에도 몇번씩 배포할 수 있다.

Page 9: Play node conference

Node restarts so quickly crashed aren't a big deal.

죽으면 어때 다시 시작하면 되지. . .

Page 10: Play node conference

Node restarts so quickly crashed aren't a big deal.

죽으면 어때 다시 시작하면 되지. . .이건 좀 오버인것 같다!

Page 11: Play node conference

마성에 빠진 JAVA 개발자들은 아직도 돌아오지 않고 있습니다. . . . . ㅠ,.ㅠ

Page 12: Play node conference

1.비동기 병렬처리 사례

“ PhantomJS 기반의 이미지 최적화 처리 시스템 ” : AWS, MQ 기반의 비동기 처리

Practices for designing scalable architecture base on node.js

Page 13: Play node conference

650 pixels

11,189 pixels

400 pixels250 pixels

쇼핑몰 상품 상세 이미지 / 웹툰 / 게시판 짤 이미지 등

Page 14: Play node conference

PhantomJShttp://phantomjs.org

Page 15: Play node conference

이미지 최적화 = Capture + Resize + SliceCPU Intensive Task=

Page 16: Play node conference

CPU core 수 만큼 Single Thread 로 처리

Multi Thread 도 어차피 CPU 시분할 처리

Page 17: Play node conference

개발 생산성, 확장성, 운영의 편의성 역시 Node.js사실은 PhantomJS 가 좋아서 . . . . .

Page 18: Play node conference

AWS EC2 instanceS3

(Bucket)

Cloud Front (Edge Location)

캡처 대상 URL 주소

PhantomJS 기반의 이미지 최적화 (capture + resize + slice)

최적화된 이미지는 CDN 을 통해 서비스

Page 19: Play node conference

AWS EC2 instances (Auto Scaling group)

S3 (Bucket)

Cloud Front (Edge Location)

Elastic Load Balancer

HTTP 요청

Page 20: Play node conference

S3 (Bucket)

Cloud Front (Edge Location)

rabbitMQ

DynamoDB

Async 요청 완료 응답

로그 저장

Page 21: Play node conference

4 instances

Page 22: Play node conference

100+ instances(일괄 배치 작업)

Page 23: Play node conference

2.실시간 메시지 처리 사례

“ socket.io 기반의 실시간 채팅 시스템 ” : zookeeper, redis 활용 : 효율적인 Weighted Consistent Hashing 구현 : 프로세스 모니터링 & 실시간 서버 모니터링

Practices for designing scalable architecture base on node.js

Page 24: Play node conference

Real-time Dashboard

Page 25: Play node conference

Instant Messenger

Page 26: Play node conference

Mobile Push Notification

Page 27: Play node conference

REAL-TIMESERVER

Page 28: Play node conference

+

Page 29: Play node conference

+

Page 30: Play node conference

+

Page 31: Play node conference

USER Management DEVICE Management

DATA Storage PUSH Notification

ACCESS Authentication SESSION Control

Page 32: Play node conference

USER Management DEVICE Management

DATA Storage PUSH Notification

ACCESS Authentication SESSION Control

Page 33: Play node conference

USER Management DEVICE Management

DATA Storage PUSH Notification

ACCESS Authentication SESSION Control

Page 34: Play node conference
Page 35: Play node conference

CHANNEL SERVER SESSION SERVER

Page 36: Play node conference

CHANNEL SERVER SESSION SERVER

Page 37: Play node conference

Apache ZooKeeper™

Page 38: Play node conference

Apache ZooKeeper™

watching node변경된 노드 정보 동기화

create EPHEMERAL node임시 노드로 서버 정보 생성

Page 39: Play node conference

Apache ZooKeeper™

The King of Coordinationapache zookeeper

- DISTRIBUTED LOCKING- SERVICE MANAGEMENT (LOAD BALANCER, DNS . . .)

Page 40: Play node conference

Apache ZooKeeper™

NEW !!

FAILED

Page 41: Play node conference
Page 42: Play node conference

서버 할당은 어떻게 하는가 ?SESSION 서버는 어떤 CHANNEL 서버를 할당해 줘야 하는지 ?

Page 43: Play node conference

A

B

C

D

value�=�HASH(key)

Service�Server

Token�Ring

CONSISTENT HASHING

Page 44: Play node conference

CONSISTENT HASHING

A

B

C

D 장애�발생�!!

value�=�HASH(key)

A�-�C�구간이�넓어서�C�에�부하가�많을�수�있다�!

Page 45: Play node conference

A

B

C

D

value�=�HASH(key)B C DABCD

D

ABCD

ABCABCD

ABCA

ABCD A

B C D

value�=�HASH(key2)

CONSISTENT HASHING

Page 46: Play node conference

A

B

C

D

B C DABCD

D

ABCD

ABCABCD

ABCA

ABCD A

B C D

value�=�HASH(key2)

value�=�HASH(key)

CONSISTENT HASHING

Page 47: Play node conference

https://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html

CONSISTENT HASHINGreplics 수에 따른 표준편차(standard deviation)

서버�노드�10�개를�1�부터�500�개�까지�relicas�로�배치�10,000�번의�consistent�Hashing�결과

Page 48: Play node conference

CONSISTENT HASHINGhttps://github.com/3rd-Eden/node-hashring

varHashRing=require('hashring');

varring=newHashRing(['127.0.0.1','127.0.0.2','127.0.0.3','127.0.0.4']);

ring.get('채팅방키’);//접속서버반환

Page 49: Play node conference

Weighted CONSISTENT HASHINGhttps://github.com/3rd-Eden/node-hashring

varHashRing=require('hashring');

varring=newHashRing({'127.0.0.1':{weight:200},'127.0.0.2':{weight:200},'127.0.0.3':{weight:200},'127.0.0.4':{weight:1000}});

ring.get('채팅방키’);//접속서버반환

Page 50: Play node conference

실시간 Weight 를 수정한다.서버 접속자 수에 따라 실시간으로 replica 수를 조정한다.

좀 더 잘하기 위해서 . . . . .

Page 51: Play node conference

http://www.slideshare.net/JohnKim0331/ss-52091187

Page 52: Play node conference

http://www.slideshare.net/JohnKim0331/ss-52091187

Page 53: Play node conference

http://www.slideshare.net/JohnKim0331/ss-52091187

Page 54: Play node conference

http://www.slideshare.net/JohnKim0331/ss-52091187

Page 55: Play node conference

Node restarts so quickly crashed aren't a big deal.

죽으면 어때 다시 시작하면 되지. . .모든게 말 처럼 간단하면 얼마나 좋겠냐 ?

Page 56: Play node conference

var spawn = require('child_process').spawn;

spawn(process.execPath, [monitorFilePath], { stdio: ['ignore', out, err], detached: true, env: { X_PID: process.pid } }).unref();

프로세스 모니터링 데몬을 실행한다.부모 프로세스와 자식 프로세스의 관계를 끊는다! 같이 죽으면 안되도록…

Page 57: Play node conference

var isRunning = function (pid) { try { return process.kill(pid, 0); } catch (e) { return e.code === 'EPERM' } };

프로세스 살아 있는지 체크한다.spawn 으로 실행한 데몬에서 PID 로 “0” 신호 보내기.

Page 58: Play node conference

모니터링 해보자.

Page 59: Play node conference

Grafana is a frontend for Graphite, InfluxDB and OpenTSDB with powerfull visualization features for time series data

Page 60: Play node conference
Page 61: Play node conference

InfluxDBdistributed, time series database with no external dependencies

Page 62: Play node conference

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

admin

Log Collector

InfluxDB

NGINX +

Page 63: Play node conference

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

admin

Log Collector

InfluxDB

NGINX +

Socket.IO (separated namespace for monitoring)

{ pid : process.pid,

arch : process.arch, platform : process.platform, uptime : process.uptime(), memory : process.memoryUsage(),

// 그 외 // 서버에 연결된 Client 수 // 체팅방 수 등

}

Page 64: Play node conference

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

admin

Log Collector

InfluxDB

NGINX +

Influx.writePoint()

Socket.IO-client

Page 65: Play node conference

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

CHAT SERVER

admin

Log Collector

InfluxDB

NGINX +

//nginx설정

auth_basic"Restricted";auth_basic_user_file/path/to/my/htpasswd/file;

Page 66: Play node conference

wrap up.1. CPU Intensive Task 는 잘 생각해봐야 한다.

- CPU core 수 만큼 실행하자. - Message 송수신하는 작업은 딱! 좋은 적용 대상이다.

2. 개발 시작부터 Scalability 를 고려해야 한다. - 서버 정보는 zookeeper (분산 코디네이터) 에 등록해서 관리하자. - 서버간 데이터 공유는 in-memory DB 또는 Data Grid 를 활용하자. - Consistent Hashing 과 같은 알고리즘 정도는 알아두자. (Weighted 도 중요하다.)

3. 효율적인 모니터링 방법을 고민해야 한다.서버의 uptime 이 중요하다. 프로세스를 모니터링하는 데몬도 고려해 보자.

Page 67: Play node conference

개발 중 오픈소스 프로젝트 . . . . .

Page 68: Play node conference
Page 69: Play node conference

https://github.com/xpush/node-xpushXPUSH - session server & channel server

http://simple.stalk.io

Page 70: Play node conference
Page 71: Play node conference
Page 72: Play node conference

https://github.com/xpush/xpush-chat

require(‘xpush’)XPUSH-CHAT

session server & channel server

https://github.com/xpush/io.stalk.adminhttps://github.com/xpush/io.stalk.static

Page 73: Play node conference

http://stalk.iohelps your internet business to communicate with customers on your website.

Page 74: Play node conference

AngularJS Full-Stack generator

Yeoman generator for creating MEAN stack applications, using MongoDB, Express, AngularJS, and Node.

https://github.com/DaftMonk/generator-angular-fullstack

lets you quickly set up a project following best practices

Page 75: Play node conference

https://github.com/almasaeed2010/AdminLTE

Page 76: Play node conference

https://github.com/creationix/nvm

NVM (Node Version Manager)https://www.jetbrains.com/editors/nodejs.jsp?ide=idea

cf. rbenv, pyenv

개발자에게 개발환경을 강요하는건 안될 일이지만..

Page 77: Play node conference

http://editorconfig.org

root = true

[*]

indent_style = space indent_size = 2

end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true

[*.md] trim_trailing_whitespace = false

https://www.gitignore.io/

.gitignore .editorconfig

Page 78: Play node conference

감사합니다. [email protected] https://www.facebook.com/JohnKim0331

// 2015.11.12