SlideShare a Scribd company logo
Scaling a Game Server
From 500 to 100,000 users
Problem
● Game runs in browser
● Real time events from server to client
● Complex game logic, changing requirements
● Game lobby shows information realtime
● Transactions on accounts involved
● Need to scale to 5000 concurrent users
● Need to store all game events from players
● High availability
Agenda – Solution
● Load balancing
● Storing logs with Cassandra
● Event driven programming with Python/Gevent
● Publish/subscribe with Redis
● Transactions with MySQL
● Performance testing
How It Started
How It Started
● C++
● Runs on a single fat server
● Logs are written to files
● Each game in a separate process
Load Balancing
Load Balancing
Load Balancer
Distributed Application
POST /login GET /account
GET /account
403 Access Denied
POST /login
200 OK
Load Balancer with Sessions
POST /login GET /account
GET /account
200 OK
POST /login
200 OK
Memory Store or Database
Load Balancing: Stateful
Send requests to same server
Based on:
- Cookies
- HTTP Headers
- Incoming IP address
- HTTP parameters
HAProxy
● Event driven
● SSL off-loading
● WebSockets support
● Easily handle the bandwidth of 10G Ethernet
with single CPU
https://www.haproxy.org/10g.html
Real Time Events from Server
WebSockets
HTTP Request Upgrade to WebSocket
(communicate like TCP connection)
WebSockets
● Communicate like raw TCP sockets
● Works from browsers without plugins
● Starts as a HTTP connection
– Works with many proxy servers, gateways etc.
– Needs a capable load balancer
– Using SSL makes the connect robust
Long Polling
Client Server
GET /events
200 OK, nothing
GET /events
Event 1
200 OK, Event 1
GET /events
30 secs 36 secs
6 secs
Long Polling
● Easy to implement
● Slightly delayed events
● More bandwidth used
● Good as a fallback or for less demanding
applications
Storing Logs
Log Storage
● Store logs in files?
● What about easy retrival of a single game log
– Store each game in a separate file?
Log Storage
● Problems with file storage:
– Poor performance due to random disk writes
– No redudancy
– Harder to accumulate and query
– Servers become stateful
Cassandra
Node
Node
Node
Node
Node Node
Key Spaces
Key A Column 1 Column 2 Column 3 Column 4 ...
Value 1 Value 2 Value 3 Value 4 ...
Key B
Key Distribution
0 – N/6
N/6 to 2N/6
2N/6 to 3N/6
3N/6 to 4N/6
4N/6 to 5N/6
5N/6 to N
N = 2^128
Replication = 2
0 – N/6, previous node
N/6 to 2N/6, previous node
2N/6 to 3N/6, previous node
N = 2^128
Read = 1, Write = N
Read = N, Write = 1
Read = Quorum, Write = Quorum
Quorum = ceil((n + 1) / 2)
Cassandra
● Fully distributed
● Replicate data on mutiple nodes
● Scale to hundreds of nodes
● Loose schema
● Handle millions of columns
● Good cluster management capablities
Event Driven Programming
Handling Requests
● With threads or processes
Event Driven Programming
● Event loop
● Example callback
while (True):
event = wait_for_event(event_subscriptions)
event.callback()
function callback() {
console.log(“Callback called, going back to event loop”);
}
setTimeout(callback, 3000);
Example Frameworks
● Javascript in Browser
● Most UI libraries (GTK+, Qt, etc.)
● Python (asyncio, gevent, Twisted)
● Node.js
● Java Netty
● Etc.
Advantages
● Performant for I/O driven applications
● Handling synchronization is easier
Disadvantages: Harder
def process():
sub_1()
io_1_sleep()
sub_2()
io_2_sql_query()
sub_3()
io_3_socket_write()
def process_1():
sub_1()
io_1_sleep(process_2)
def process_2()
sub_2()
io_2_sql_query(process_3)
def process_3()
sub_3()
io_3_socket_write()
Threaded Program Event Driven Program
Disadvantages: Libraries
● Needs different set of libraries
● Written with event loop in mind
Python & Gevent
>>> import gevent
>>> from gevent import socket
>>> urls = ['www.google.com', 'www.example.com',
'www.python.org']
>>> jobs = [gevent.spawn(socket.gethostbyname, url)
for url in urls]
>>> gevent.joinall(jobs, timeout=2)
>>> [job.value for job in jobs]
['74.125.79.106', '208.77.188.166', '82.94.164.162']
Python & Gevent
from gevent.server import StreamServer
def echo(socket, address):
socket.sendall(b'Welcome to the echo server!rn')
handle = socket.makefile(mode='rb')
while True:
line = handle.readline()
if line.strip().lower() == b'quit':
break
socket.sendall(line)
handle.close()
if __name__ == '__main__':
server = StreamServer(('0.0.0.0', 16000), echo)
server.serve_forever()
Event Driven Program with Gevent
def process():
sub_1()
io_1_sleep()
sub_2()
io_2_sql_query()
sub_3()
io_3_socket_write()
def process():
sub_1()
io_1_sleep()
sub_2()
io_2_sql_query()
sub_3()
io_3_socket_write()
Threaded Program Gevent Program
Event Driven Program with AsyncIO
def process():
sub_1()
io_1_sleep()
sub_2()
io_2_sql_query()
sub_3()
io_3_socket_write()
def process():
sub_1()
yield io_1_sleep()
sub_2()
yield io_2_sql_query()
sub_3()
yield io_3_socket_write()
Threaded Program AsyncIO Program
Lobby Information
Lobby Information
● Need to collect updates from all games
● Need to send it to every user
● Thousands of games
● Changing every second
● Tens of thousands of users
● Need to see updates every second
Publish/Subscribe
Publish Subscribe
Event A Event A
Event A
Event A
Game Server Lobby Server
Redis
● Key/value store
● Supports complex data structures for value
● Mainly for memory
● Also can persist data on disk
● Supports publish/subscribe
Redis High Availability
Master ReplicationReplication
Sentinel Sentinel Sentinel
SyncSync
Sending Updates
● Send updates over WebSocket
● Send aggregates
● Send only delta of changes
Transactions
Transactions
● Multiple operations on user accounts
● Should all be atomic
● In other words, ACID compliance required
MySQL
Master
Read/Write
MySQL Replication
Master ReplicationReplication
SyncSync
Read/WriteRead Only Read Only
MySQL Cluster
Query Node
Query Node
Query Node
Node 2Node 1
Partition 1
Node 4Node 3
Partition 2
MySQL Cluster - Data Layer
Performance Testing
Performance Testing Framework
● A bot that simulates game play
– With Python/Gevent
● Another framework
– Locust
Final Architecture
Cassandra
MySQL
Redis
Lobby Servers Game Servers
Load Balancers
Clients
Services API
License
● License: Creative Commons Attribution Share-Alike 4.0
International License
● Copyright: Sunil Mohan Adapa <sunil at medhas dot org>
● Icons from the Noun Project by:
– Alena
– Unlimic
– Alex WaZa
– Setyo Ari Wibowo
– Royyan Wijaya
– Under Creative Commons Attribution 3.0 United States License
Scaling a Game Server: From 500 to 100,000 Users

More Related Content

PDF
Patologias de vulva
PPTX
5. metodos anticonceptivos para adolescentes
PDF
MySQL Parallel Replication by Booking.com
PDF
Large volume data analysis on the Typesafe Reactive Platform - Big Data Scala...
PPTX
HBaseCon 2015: OpenTSDB and AsyncHBase Update
PDF
Haproxy - zastosowania
PDF
EVCache & Moneta (GoSF)
PPTX
Advanced Replication
Patologias de vulva
5. metodos anticonceptivos para adolescentes
MySQL Parallel Replication by Booking.com
Large volume data analysis on the Typesafe Reactive Platform - Big Data Scala...
HBaseCon 2015: OpenTSDB and AsyncHBase Update
Haproxy - zastosowania
EVCache & Moneta (GoSF)
Advanced Replication

Similar to Scaling a Game Server: From 500 to 100,000 Users (20)

PDF
Optimizing Tiered Storage for Low-Latency Real-Time Analytics at AI Scale
PDF
Scalable Socket Server by Aryo
ODP
Node js lecture
PDF
How to provide enterprise high availability with MariaDB Platform
PDF
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
PPTX
Logs @ OVHcloud
PDF
Fast Incremental Backups with Percona Server and Percona XtraBackup / PLMCE 2014
PPTX
MongoDB for Time Series Data Part 3: Sharding
PDF
2013 london advanced-replication
PDF
EVCache: Lowering Costs for a Low Latency Cache with RocksDB
PPTX
Lightweight Transactions at Lightning Speed
PDF
MySQL/MariaDB Parallel Replication: inventory, use-case and limitations
PPTX
Replication and Replica Sets
PDF
Data Grids with Oracle Coherence
PDF
Mongodb workshop
PDF
The powerful toolset of the go-mysql library
PPTX
Replication and Replica Sets
PDF
Tweaking performance on high-load projects
PDF
MySQL 5.7 in a Nutshell
PDF
Replication MongoDB Days 2013
Optimizing Tiered Storage for Low-Latency Real-Time Analytics at AI Scale
Scalable Socket Server by Aryo
Node js lecture
How to provide enterprise high availability with MariaDB Platform
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
Logs @ OVHcloud
Fast Incremental Backups with Percona Server and Percona XtraBackup / PLMCE 2014
MongoDB for Time Series Data Part 3: Sharding
2013 london advanced-replication
EVCache: Lowering Costs for a Low Latency Cache with RocksDB
Lightweight Transactions at Lightning Speed
MySQL/MariaDB Parallel Replication: inventory, use-case and limitations
Replication and Replica Sets
Data Grids with Oracle Coherence
Mongodb workshop
The powerful toolset of the go-mysql library
Replication and Replica Sets
Tweaking performance on high-load projects
MySQL 5.7 in a Nutshell
Replication MongoDB Days 2013
Ad

More from GeekNightHyderabad (20)

PPTX
Testing strategies in microservices
PDF
Metaprogramming ruby
PPTX
Scaling enterprise digital platforms with kubernetes
PDF
FreedomBox & Community Wi-Fi networks
PDF
Rendezvous with aucovei (autonomous connected car)
PDF
Role of AI & ML in beauty care industry
PPTX
Breaking down a monolith
PDF
Design lean agile_thinking presentation
PPTX
Scaling pipelines
PDF
Blockchain beyond bitcoin
PPTX
PPTX
Hardware hacking and internet of things
PDF
Spring to Cloud - REST To Microservices
PDF
Serverless
PPTX
Building Cloud Native Applications Using Spring Boot and Spring Cloud
PPTX
Progressive Web Applications - The Next Gen Web Technologies
PPTX
Big Data - Need of Converged Data Platform
PDF
Building a Data Lake - An App Dev's Perspective
PPTX
Understanding the Intelligent Cloud
PDF
GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Testing strategies in microservices
Metaprogramming ruby
Scaling enterprise digital platforms with kubernetes
FreedomBox & Community Wi-Fi networks
Rendezvous with aucovei (autonomous connected car)
Role of AI & ML in beauty care industry
Breaking down a monolith
Design lean agile_thinking presentation
Scaling pipelines
Blockchain beyond bitcoin
Hardware hacking and internet of things
Spring to Cloud - REST To Microservices
Serverless
Building Cloud Native Applications Using Spring Boot and Spring Cloud
Progressive Web Applications - The Next Gen Web Technologies
Big Data - Need of Converged Data Platform
Building a Data Lake - An App Dev's Perspective
Understanding the Intelligent Cloud
GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Ad

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation theory and applications.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Programs and apps: productivity, graphics, security and other tools
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I
sap open course for s4hana steps from ECC to s4
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
Programs and apps: productivity, graphics, security and other tools

Scaling a Game Server: From 500 to 100,000 Users