71
Building fast, scalable game server in node.js Charlie Crane @xiecc

Building fast,scalable game server in node.js

Embed Size (px)

DESCRIPTION

At talk in jsconf asia

Citation preview

Page 1: Building fast,scalable game server in node.js

Building fast, scalable game server in node.js

Charlie Crane@xiecc

Page 2: Building fast,scalable game server in node.js

Who am I

NASDAQ: NTES

Senior engineer, architect(8 years) in NetEase Inc. , developed many web, game products

Recently created the open source game server framework in node.js: pomelo

Page 3: Building fast,scalable game server in node.js

Top cities for npm moduleshttps://gist.github.com/substack/7373338

Page 4: Building fast,scalable game server in node.js

AgendaState of pomeloScalabilityFrameworkPerformanceRealtime application

Page 5: Building fast,scalable game server in node.js

The state of pomelo

Fast, scalable, distributed game server framework for node.js

Open sourced in 2012.11.20 Newest version: V0.7

https://github.com/NetEase/pomelo

Page 6: Building fast,scalable game server in node.js

Pomelo position Game server framework

Mobile gameWeb gameSocial gameMMO RPG(middle scale)

Realtime application server framework

Realtime Multiple User Interaction

Page 7: Building fast,scalable game server in node.js

State of pomeloPomelo is not a single projectAlmost 40 repos in total

Page 8: Building fast,scalable game server in node.js

Framework ---

Page 10: Building fast,scalable game server in node.js

Success casesCard Game — ShenMu

http://shenmu.iccgame.com/index.shtml

Page 11: Building fast,scalable game server in node.js

Success casesCombat Strategy Game — Ancient songs

http://www.ixuanyou.com/

Page 12: Building fast,scalable game server in node.js

Q&A

Success cases

http://wap.youth.cn/bangbang/

Realtime Communication Tools —ZhongQingBangBang

Page 13: Building fast,scalable game server in node.js

Success cases

The magic card hunter https://itunes.apple.com/cn/app/id6649640688

Beauty Fried golden flower http://app.xiaomi.com/detail/41461

Texas Hold ’ em poker http://www.yiihua.com/

Metal Slug http://www.17erzhan.com/ww2/ww2index.php

Coal instant messaging http://www.mtjst.com

Yong Le golden flower http://pan.baidu.com/s/1pCmfD

17 Bullfight http://app.91.com/Soft/Detail.aspx?Platform=iPhone

Page 14: Building fast,scalable game server in node.js

Job huntings

Page 15: Building fast,scalable game server in node.js

AgendaThe state of pomeloScalabilityFrameworkPerformanceRealtime appliation

Page 16: Building fast,scalable game server in node.js

Demo firsthttp://pomelo.netease.com/lordofpomelo

Page 17: Building fast,scalable game server in node.js

Scalability---Web and Game serverWeb server unlimited scalability

Game server

World record: world of tanks(bigworld), 74,536 online usersMMORPG : 7k-8k maximum

Why?

Page 18: Building fast,scalable game server in node.js

Why does game server not scale?

Reason 1: Long connection

Response time

Web response time : 2000ms Game 、 realtime web : <100ms

Message direction

web: request/reponse game: request/push/broadcast, bi-direction

Page 19: Building fast,scalable game server in node.js

How to solveNode.js to rescue

Perfect for:Network-insentive applicationReal-time applicationHolding connections

Mqtt: 120,000 connections, 1 process, 10K/connectionSocket.io: 25,000 connections, 1 process, 50K/connection

Page 20: Building fast,scalable game server in node.js

Why does game server not scaleReason 2: stateful

Web app -- statelessNo coordinates, no adjacencyPartition randomly, stateless

Game server -- statefulHave coordinate, adjacencyPartition by area, stateful

Page 21: Building fast,scalable game server in node.js

How to solve No good solution in technical level

Depending on game designDon’t make one area too crowdedBalance of area

Page 22: Building fast,scalable game server in node.js

Too crowded area

Page 23: Building fast,scalable game server in node.js

Disperse player

Page 24: Building fast,scalable game server in node.js

Why does game server not scale

MESSAGES IN

1

MESSAGES OUT

1

Reason 3: Broadcast

1 1

Page 25: Building fast,scalable game server in node.js

Why does game server not scale

MESSAGES IN

2

MESSAGES OUT

4

1 2

12

Page 26: Building fast,scalable game server in node.js

Why does game server not scale

MESSAGES IN

4

MESSAGES OUT

161 4

14

11

4 4

Page 27: Building fast,scalable game server in node.js

Why does game server not scale

MESSAGES IN

100

MESSAGES OUT

100001 100

1100

11

100 100

Page 28: Building fast,scalable game server in node.js

Why does game server not scale

MESSAGES IN

1000

MESSAGES OUT

10000001 1000

11000

11

1000 1000

Page 29: Building fast,scalable game server in node.js

1 、 How to solve --- broadcastAOI --- area of interested module:

pomelo-aoi

Page 30: Building fast,scalable game server in node.js

2 、 How to solve -- broadcastSplit process, seperate load , frontend is

statelessFrontend(connector)

Backend(area)

Single Process

broadcast

Game logic

Page 31: Building fast,scalable game server in node.js

3 、 How to solve – broadcast

Game design, don’t make one place(in area) too crowded

Map sizeCharacter densityDisperse born place

Page 32: Building fast,scalable game server in node.js

Disperse born place

Page 33: Building fast,scalable game server in node.js

4 、 How to solve -- pushScheduler

Direct : send message directlyBuffer: buffer message in array, and flush

it every 50msOther Strategies: flexible, depending on

client number

app.set(‘pushSchedulerConfig’, { scheduler: new MessageScheduler(app);});

Page 34: Building fast,scalable game server in node.js

Why does game server not scaleReason 4: Tick (game loop)

setInterval(tick, 100)

What does every tick do?Update every entity in the

scene(disappear , move, revive)Refresh monsterDriving ai logic(monster, player)

Tick must be far less than 100ms

Page 35: Building fast,scalable game server in node.js

Problem of tick The entity number should be limited

Pay attention to update algorithm: AI etc.

GC, full gc should be limitedV8 is good at GC when memory is under

500M, even full GCMemory must be limitedTry to divide process

Page 36: Building fast,scalable game server in node.js

At last--- runtime architecture

Page 37: Building fast,scalable game server in node.js

How to solve complexityToo … complicated?

solution: framework

Page 38: Building fast,scalable game server in node.js

AgendaThe state of pomeloScalabilityFrameworkPerformanceRealtime application

Page 39: Building fast,scalable game server in node.js

Pomelo Framework

The essence of pomelo:

A distributed, scalable, realtime application framework.

Page 40: Building fast,scalable game server in node.js

Framework --- design goalAbstract of servers(processes)

Auto extend server typesAuto extend servers

Abstract of request/response and broadcast/pushZero config request Simple broadcast api

Servers communication---rpc framework

Page 41: Building fast,scalable game server in node.js

Framework -- some featuresServer scalabilityNetworkPlugins

Page 42: Building fast,scalable game server in node.js

Server scalability

Page 43: Building fast,scalable game server in node.js

Server Scalability-- dynamic extensions

pomelo add host=[host] port=[port] id=[id] serverType=[serverType]

node app.js env=production host=[host] port=[port] id=[id] serverType=[serverType]

Page 44: Building fast,scalable game server in node.js

Network

Connector

SIOConnector HybridConnector

Pomelo component Loader

MQTTConnector

Socket.io client Socket, websocketclient

Mobile client

Page 45: Building fast,scalable game server in node.js

Network--Message encode, decode

Page 46: Building fast,scalable game server in node.js

Network--original protobufOriginal protobuf

Page 47: Building fast,scalable game server in node.js

Network--pomelo protobufOur protobuf

Page 48: Building fast,scalable game server in node.js

Network -- Proto compressConfig:

Page 49: Building fast,scalable game server in node.js

Network---protobuf definitionHandler return json, auto encoded to

protobuf

Page 50: Building fast,scalable game server in node.js

Network--pomelo protobufhttps://github.com/pomelonode/pomelo-protobuf

As an independent project, supporting unity3d, iOS, android, C

Page 51: Building fast,scalable game server in node.js

Network -- Pomelo Netflow – V0.2Version 0.2, 400 online usersOut: 3645Kb/S in: 270Kb/s

Page 52: Building fast,scalable game server in node.js

Network -- Pomelo Netflow – V0.3Version 0.3, 400 online usersOut: 925KB/S, in: 217KB/S, 25% Netflow of 0.2

Page 53: Building fast,scalable game server in node.js

PluginsA mechanism to extend framework

Example:pomelo-status-plugin , online status plugin,

based on redis

Page 54: Building fast,scalable game server in node.js

Plugins example -- Reliabilitypomelo-masterha-plugin, based on

zookeeper, master HA

Servers auto reconnect Zookeeper

Master

Slave

Slave

Page 55: Building fast,scalable game server in node.js

AgendaState of pomeloScalabilityFrameworkPerformanceRealtime application

Page 56: Building fast,scalable game server in node.js

Performance --- overviewThe performance varies largely in different

games, applications

The variationApplication type: Game or realtime

applicationGame type: round or realtime fight, room or

infiniteGame design: Map size, character density,

balance of areasTest parameters: Think time, test action

Page 57: Building fast,scalable game server in node.js

Performance --- reference data Online users per process

next-gen: support 20,000 concurrent players in one area

World record – not one areaworld of tanks(bigworld), 74,536 online

users

But in real world(MMO rpg)The real online data: maximum 800 concurrent

users per area, 7,000 concurrent users per group game servers

Page 58: Building fast,scalable game server in node.js

Performance--Server configurationOpenstack virtual machine

Page 59: Building fast,scalable game server in node.js

Performance --- stress testingUse Case 1: fight

Stress on single area(area 3), increasing step by step, login every 2 seconds

Game logic: attack monstors or other players every 2~5 seconds

Page 60: Building fast,scalable game server in node.js

Performance --- stress testing

Page 61: Building fast,scalable game server in node.js

Performance--result486 onlines

Page 62: Building fast,scalable game server in node.js

Performance -- topUsing toobusy, limit cpu 80%

Page 63: Building fast,scalable game server in node.js

Performance – stress testUse case 2 – move

Stress on single area(area 3), increasing step by step, login every 2 seconds

Game logic: move in the map every 2~5 seconds

Page 64: Building fast,scalable game server in node.js

Performance – stress test800 online users

Page 65: Building fast,scalable game server in node.js

Performance --- stress testingUse Case 3: fight & move

Stress on single area(area 3), increasing step by step, login every 2 seconds

Game logic: 50% attack monstors or other players every 2~5 seconds, 50% move around

Result: 558 onlines in one area

Page 66: Building fast,scalable game server in node.js

AgendaState of pomeloScalabilityFrameworkPerformanceRealtime application

Page 67: Building fast,scalable game server in node.js

Realtime app and gameMany success stories in realtime

applicationMessage push platform in NetEaseZhongQinBangBang

Not that frequent messages as gameDo do need that realtime (less than

50ms), we can use different pushScheduler strategy

Much more scalable than game

Page 68: Building fast,scalable game server in node.js

Message push platform

Supporting more than 10,000,000 online users

Products using message push platformlove.163.commusic.163.comnews.163.com note.youdao.comgame.163.comyuehui.163.comyuedu.163.com

……………………………………

Page 69: Building fast,scalable game server in node.js

Message push platformAMQP

Receiver publish APNS publisher

Channel push, socket

Connector Connector Connector

Android client iOS client

mqtt mqtt

Redis,Device

LVS

Receiver

Receiver

ReceiverAPNS publisher

Web client

socket.io

Page 70: Building fast,scalable game server in node.js

Message push systemsHigh load, More than 10,000,000 online

usersRealtime, broadcast to 5 million users in

1minuteReliability, qos=1 in any conditionsSave power and networkSupport all the clients

iOS(APNS, mqtt)android(mqtt)browser(socket.io)windows desktop application(mqtt)

Page 71: Building fast,scalable game server in node.js

Q&A

@xiecchttps://github.com/NetEase/pomelo