scalable node.js with redis store

41
Scalable Node.js with Redis Store Node.js 大阪 勉強会 2014.6.14

Upload: kamiyam-

Post on 10-May-2015

4.500 views

Category:

Education


6 download

DESCRIPTION

Node.js大阪勉強会 #npp02

TRANSCRIPT

Page 1: Scalable Node.js with Redis Store

Scalable Node.js with Redis Store

Node.js大阪 勉強会 2014.6.14

Page 2: Scalable Node.js with Redis Store

自己紹介

• かみやん (Twitter@kamiyam)

http://nantokaworks.com

• Engineer

• たまにカメラの人

Page 3: Scalable Node.js with Redis Store

Production Environment

Page 4: Scalable Node.js with Redis Store

本番環境

Page 5: Scalable Node.js with Redis Store

Production Environment

同時接続処理に強いNode.js

Page 6: Scalable Node.js with Redis Store

Production Environment

しかしそれなりの対応が必要

Page 7: Scalable Node.js with Redis Store

Scalable Node.js

Page 8: Scalable Node.js with Redis Store

AWSCluster

Qilin

pm2Heroku

Page 9: Scalable Node.js with Redis Store

AWSCluster

Qilin

pm2Heroku

ModuleEnvironment

Page 10: Scalable Node.js with Redis Store

AWSCluster

Qilin

pm2Heroku

Page 11: Scalable Node.js with Redis Store

Environment/Module にかかわらず 複数のプロセスが立ち上がる

Page 12: Scalable Node.js with Redis Store

Multiple Process

Page 13: Scalable Node.js with Redis Store

プロセスインスタンスが 複数発生した場合の2つの課題

Page 14: Scalable Node.js with Redis Store

Session Management

Page 15: Scalable Node.js with Redis Store

Express Default Session

$ NODE_ENV=production node app.js !Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and obviously only work within a single process.

Page 16: Scalable Node.js with Redis Store

Session use LoadBalancer

ログイン管理の セッションはどうなっている?

Page 17: Scalable Node.js with Redis Store

Sticky Sessions  

Page 18: Scalable Node.js with Redis Store

WebSocketは…?

Page 19: Scalable Node.js with Redis Store

WebSocket HandShake

Socket.io(と言うかWebSocket)の接続時

HTTP(S)プロトコル-> WS(S)プロトコル となり二種類のコネクションが発生する

Page 20: Scalable Node.js with Redis Store

WebSocket HandShake

複数のプロセスが存在する場合 必ず同じプロセスインスタンスに 接続できるとは限らない

Page 21: Scalable Node.js with Redis Store

結果 Socket.io handshake 失敗

Page 22: Scalable Node.js with Redis Store

DEMO

Page 23: Scalable Node.js with Redis Store

WebSocket HandShake

プロセス単体それぞれで管理を行わず プロセス間での情報共有をRedisで行う

Page 24: Scalable Node.js with Redis Store

Socket.io Event

Page 25: Scalable Node.js with Redis Store

Socket.io の プロセス間共有

Socket.io Message Sharing Scopeが問題となる

プロセスインメモリでMessageEmittingを行うため

イベントが発生する/しないという問題が発生する

Page 26: Scalable Node.js with Redis Store

プロセス単体それぞれで管理を行わず プロセス間での情報共有をRedisで行う

(2回目)

Socket.io の プロセス間共有

Page 27: Scalable Node.js with Redis Store

Redis-Server による解決

Page 28: Scalable Node.js with Redis Store

Redis

揮発性 Key-Value Store(KVS)

Page 29: Scalable Node.js with Redis Store

connect-redis

Page 30: Scalable Node.js with Redis Store

Cation

connect-redisのバージョンに注意

Page 31: Scalable Node.js with Redis Store

connect-redis >= 2.0.0 support only express >= 4.0.0.  Use connect-redis 1.4.7 for express 3x.

connect-redis

Page 32: Scalable Node.js with Redis Store

Socket.io Event Emitter

Page 33: Scalable Node.js with Redis Store

use RedisStore

Page 34: Scalable Node.js with Redis Store

Socket.io の プロセス間共有

Page 35: Scalable Node.js with Redis Store

□ Socket.io 0.x系 redis store □ Socket.io 1.x系 socket.io-redis

RedisStore

Page 36: Scalable Node.js with Redis Store

□ Socket.io 0.x系 redis store □ Socket.io 1.x系 socket.io-redis

RedisStore

Oh…

Page 37: Scalable Node.js with Redis Store

□ Socket.io 0.x系 redis store □ Socket.io 1.x系 socket.io-redis

RedisStore

Koaといい Socket.io v1.0といい また新たな過渡期に 突入するわけですね

Page 38: Scalable Node.js with Redis Store
Page 39: Scalable Node.js with Redis Store

まとめ

• Multiple Process

• Session Management

• Event-Emitter Sharing

Page 40: Scalable Node.js with Redis Store

参考• Scaling real-time apps on Cloud Foundry (using Redis) [GitHub] 

https://github.com/rajaraodv/redispubsub

• SNode.js + Socket.IO + pm2でデーモン化とクラスタリング

http://tomowatanabe.hatenablog.com/entry/2014/01/31/124740

• Node におけるスケールアーキテクチャ考察(Scale 編)

http://jxck.hatenablog.com/entry/20110618/1308378963

• Socket.IO or WebSocket を AmazonELB でバランスする検証

http://jxck.hatenablog.com/entry/20120228/1330444857

Page 41: Scalable Node.js with Redis Store

ご清聴ありがとうございました