NodeJS

Load Balancing

Nairi

Harutyunyan

nairihar99@gmail.com

@nairihar

Content

  • NodeJS&Nginx
  • Socket Servers
  • Socket Servers&Redis
  • Other Load Balancers

Imagine

You have a NodeJS server with 3k requests per second

Now You have 30k requests per second

Problem

Lots of requests

Load Balancer

NGINX

Nginx&Node

NodeJS

:8000

NodeJS

:8001

NodeJS

:8002

NGINX

:3000

r1

r2

r3

r4

r5

r1

r2

r3

r4

r5

Let's do that

Socket Server

polling/websocket

Problem

Connections/Polling

Nginx&Node(Socket)

NodeJS

:8000

NodeJS

:8001

NodeJS

:8002

NGINX

:3000

u1

req 1

req 2

polling req 1

polling req 2

res 1

Session ID unknown

Solution

Stop balance after user's first request

Nginx&Node(Socket)

NodeJS

:8000

NodeJS

:8001

NodeJS

:8002

NGINX

:3000

u1

polling req 1

polling req 2

polling req 1

polling req 2

Another Problem

Server 1 can't send data to user 2

Nginx&Node(Socket)

NodeJS

:8000

NodeJS

:8001

NodeJS

:8002

NGINX

:3000

u1

u1 connected to s1

u1 emits to s1 for u2

u1 connect to socket

u1 emit data for u2

u2

u2 connect to socket

u2 connected to s2

Error

Solution

REDIS

Redis

*key-value store

PUB/SUB

REDIS

NodeJS

:8000

NodeJS

:8001

subscribe

publish

publish

subscribe

Solution

Use Redis PUB/SUB with Sockets

Nginx&Node(Socket)&Redis

NodeJS

:8000

NodeJS

:8001

NodeJS

:8002

NGINX

:3000

u1

u2

REDIS

DEMO

Other Load Balancers

Thank You!

nairihar99@gmail.com

@nairihar

Made with Slides.com