Scalable Socket Server by Aryo

241 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
241
On SlideShare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Scalable Socket Server by Aryo

  1. 1. @agatestudio Scalable Socket Server Aryo Knight Agate Studio
  2. 2.  Built on Chrome’s javascript runtime  Event driven (non blocking I/O model)  Multiplatform (Mac OSX, Linux, Windows)  Only single-threaded
  3. 3. // module required var net = require('net'); // create server var svr = net.createServer(function(sock) { console.log('Connected: ' + sock.remoteAddress + ':' + sock.remotePort); // error listener & handler function sockOnError(err) { console.log('Socket Error: ' + err.stack + 'n'); } sock.on('error', sockOnError); // server get data from client function sockOnData(data) { console.log('Data: ' + data.toString()); // simple echo back to client sock.write(data, function() { // called on write finished } } sock.on('data', sockOnData); // client disconnected function sockOnEnd() { console.log('Disconnected'); } sock.on('end', sockOnEnd); // function to disconnect player function destroy() { sock.removeListener('data', sockOnData); sock.removeListener('end', sockOnEnd); sock.removeListener('error', sockOnError); sock.destroy(); sock = null; console.log("Destroyed"); } } // listen on port and ip svr.listen(SERVER_PORT, SERVER_ADDR);
  4. 4.  Only use single thread › Not take advantages from multiple core CPU › Slow response on busy server (lot of clients or messages) › Delay become unacceptable
  5. 5.  Using multiple server, even on one computer to utilize other CPU cores  Use message queue as a central › We are using Redis in this slide  Use load balancer to spreading request from client to our servers › Using HAProxy in this slide
  6. 6.  Message Queue › Redirect message from all servers to one database  Advanced key-value store › Pros, very fast to write and read › Cons, data is not persistent  Since we’re using to centralize data, persistency in not important › Use persistent database instead to store client data  Use publisher/subscriber to store and notify to other server
  7. 7.  HTTP/TCP load balancer › Use to distribute request from many clients  Fast, efficient, and stable › In terms of processor and memory usage  Easy to implement › Using file config  Can be used to implement blocker › Like DDOS protection › Client can’t directly connect to actual server
  8. 8. global log 127.0.0.1 local0 notice maxconn 2000 user haproxy group haproxy defaults log global mode http option httplog option dontlognull retries 3 option redispatch timeout connect 5000 timeout client 10000 timeout server 10000 listen nodeserver :7070 mode tcp option tcplog balance roundrobin server nodeserver1 127.0.0.1:7071 server nodeserver2 127.0.0.1:7072 timeout client 60s timeout server 60s timeout queue 10s timeout connect 4s
  9. 9.  Single computer with 4 CPU cores › Redis listen to 127.0.0.1:6379 (Multiple Core) › Server 1 listen to 127.0.0.1:7071 (1 Core) › Server 2 listen to 127.0.0.1:7072 (1 Core) › HAProxy listen to 172.16.1.9:7070 (Multiple Core)  HAProxy listen to IP public and distribute to server 1 & 2  Server 1 & 2 store & receive message data to & from redis (publish&subscribe)  Server 1 & 2 send back to client through HAProxy

×