SlideShare a Scribd company logo
COMBINING  THE  STRENGTHS  OF  
      ERLANG  AND  RUBY
      erlounge  Berlin  February  2012  –  Mar4n  Rehfeld
Game  Server  for  Upcoming  Wooga  Game

What  is  a  game  server?
• provide  HTTP  API  to  actual  game  („client“)
• validate  API  calls  against  current  state  &  game  logic
  • API  calls  will  modify  the  game  state
• make  game  state  persistent
Game  Server  for  Upcoming  Wooga  Game

It  will  be  a  stateful  game  server
• one  process  per  ac4ve  user  gaming  session
   • the  process  holds  the  current  state  and  is  the  only  one  that  can  modify  it  
       (strong  encapsula:on)
   • the  process  handles  all  API  calls  for  the  given  user  one  a=er  the  other  
       (concurrency  control  through  actor  model)
   • the  process  loads  the  game  state  from  storage  and  writes  it  back  
       periodically  and  on  process  termina:on  (:meout  =  user  stopped  playing)
Game  Server  for  Upcoming  Wooga  Game

Details  on  the  basic  idea:
Awesome  presenta4on  on  the
Magic  Land  game  server  by
@knu4n  &  @hungryblank
hZp://www.slideshare.net/wooga/from-­‐0-­‐to-­‐1000000-­‐daily-­‐users-­‐with-­‐erlang
Our  Goals

• Get  most  of  the  benefits  from  wooga’s  pure-­‐erlang  game  
  server  (Magic  Land)
  • especially  func:onal  approach  for  game  state  
     encapsula:on  and  transforma:on  +  concurrency  control
• But:  Keep  Object-­‐Oriented  approach  for  modelling  the  
  game  logic
Expected  OO-­‐Benefits

• Rapid  development
• concise  &  expressive  syntax
• leverage  exis4ng  know  how
• but:  keep  the  OO  part  stateless  and  side-­‐effect  free
  to  avoid  the  usual  traps  &  pidalls
Target  Architecture
                                                                                             Authority for:
                             Load Balancer
                                                                                  "What app server is responsible for
                                                                                          current session?"




           App Server Node                     App Server Node                              App Server Node


               Erlang VM                         Erlang VM                                    Erlang VM


                                                                            ...



      Ruby     Ruby       Ruby          Ruby      Ruby       Ruby                   Ruby       Ruby       Ruby
                      ...                                ...                                          ...
      Worker   Worker     Worker        Worker    Worker     Worker                 Worker     Worker     Worker




                                                   Shared Storage

                              for game state snapshots and for storing cold game sessions
Example  Game  Ac4on  in  Ruby
       URL

game_action '/:actor/fruit_tree/self/shake',
            :affects => [:fruit_trees, :user] do |response|

  x, y = params[:x], params[:y]
  fruit_trees[x, y].shake                      affected  parts  of
end
                                                the  game  state
• DSL-­‐like  defini4on  of  game  ac4on
• skinny  as  controllers  should  be  8-­‐)
Example  Model  in  Ruby
                                 Inheritance
class FruitTree < Tree
  property :last_shake_time, :type => Integer, :default => 0              DSL-­‐like  defini4on
  property :collectable_fruit_count, :type => Integer, :default => 0
                                                                          of  persistent  state
  def shake
    raise Error::Validation, "FruitTree at (#{x}, #{y}) has no fruit" unless carries_fruit?

    session.user.xp += 1
    session.user.energy -= 1
    self.last_shake_time = game_time
    self.collectable_fruit_count = config.fruit_count
  end
  # ...
end

• easily  unit  testable
• minimal  amount  of  code
erlang  talking  to  Ruby

Some  op4ons
• erlectricity  hZps://github.com/mojombo/erlectricity:
  Can  talk  erlang  binary  protocol  to  Ruby  processes  through  erlang  
  ports
• ernie  hZps://github.com/mojombo/ernie:
  Remote  func4on  call  using  the  BERT-­‐RPC  protocol  to  either  Ruby  or  
  na4ve  erlang  processes
• ZeroMQ  <hZp://www.zeromq.org/>:
  awesome  brokerless  queue  transport  layer,  connects  30+  languages
ZeroMQ

Pro                                    Con
• loose  coupling  of  erlang  and     • yet  another  layer
  Ruby  through  queues
  • easily  deploy  new  Ruby  
      code  without  touching  
      erlang
• allows  flexible  transports,  
  even  accross  machines
Connec4ng  the  Dots

• use  Mongrel2  hZp://mongrel2.org/  protocol  &  ZeroMQ  setup
• erlang:  emongrel2  hZps://github.com/hungryblank/emongrel2
• Ruby
  • rack-­‐mongrel2  fork  hEps://github.com/khiltd/khi-­‐rack-­‐mongrel2
  • rack  protocol  hEp://rack.rubyforge.org
  • Sinatra  hEp://www.sinatrarb.com/

➡ essen4ally  we  are  speaking  HTTP  over  ZeroMQ
  and  can  hook  up  any  Rack-­‐based  Ruby  web  framework
Setup  Overview



                 server                                 worker


       session                                          worker
                          sender     Push/Pull  Queue


                                        1:n             worker

       session
                                                        worker
        ...               receiver    Pub/Sub  Queue


                                        m:n              ...
       session
                                                        worker
How  does  the  game  state  look  like?

• Has  many  parts,  each  part  has  a  name  (e.g.  fruit_trees)  and  some  
   content
   • this  is  a  performance  op:miza:on,  so  that  we  don't  need  to  send  the  
      complete  state  back  and  forth  for  every  call
• erlang  does  not  care  about  the  content  (binary  data)
• actually  the  content  contains  serialized  objects  (e.g.  a  
   MapObjectCollection  containing  FruitTree  members)

• erlang  does  need  to  know,  what  game  ac4on  needs  what  state  parts
Looking  back  at  the  Game  Ac4on
game_action '/:actor/fruit_tree/self/shake',
            :affects => [:fruit_trees, :user] do |response|

  x, y = params[:x], params[:y]
  fruit_trees[x, y].shake                       affected  parts  of
end
                                                 the  game  state


• Ruby  knows  the  mapping  of  game  ac4ons  to  affected  state  parts
• pushes  the  mapping  on  startup  &  makes  it  available  on  request
Aside:  Efficient  Game  State  Access

• considered  different  serializa4on  formats  (e.g.  JSON,  
   MessagePack)
• especially  for  large  collec4ons  of  objects  we  wanted  to  avoid  
   parsing  everything  to  extract  a  single  object
• chose  TNetstrings  hZp://tnetstrings.org/  as  a  serializa4on  
   format  (length  prefix  helps  searching/lazy  parsing)
• built  a  lazy  parser/encoder  for  it    in  C  for  speed  :-­‐)
   hZps://github.com/wooga/lazy_tnetstring
Q  &  A

Mar4n  Rehfeld
 @klickmich

More Related Content

PDF
FunctionalConf '16 Robert Virding Erlang Ecosystem
PDF
Erlang factory SF 2011 "Erlang and the big switch in social games"
PDF
Erlang factory 2011 london
PDF
Erlang as a cloud citizen, a fractal approach to throughput
PDF
Architecture Evolution at Wooga (AWS Cloud Computing for Developers,)
PDF
Xen_and_Rails_deployment
PPTX
Akka.net versus microsoft orleans
PPT
Introduction to the intermediate Python - v1.1
FunctionalConf '16 Robert Virding Erlang Ecosystem
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory 2011 london
Erlang as a cloud citizen, a fractal approach to throughput
Architecture Evolution at Wooga (AWS Cloud Computing for Developers,)
Xen_and_Rails_deployment
Akka.net versus microsoft orleans
Introduction to the intermediate Python - v1.1

What's hot (12)

PPTX
Python Raster Function - Esri Developer Conference - 2015
PPTX
The Actor Model - Towards Better Concurrency
PDF
Intro to elixir and phoenix
PPTX
Kubernetes at NU.nl (Kubernetes meetup 2019-09-05)
PPTX
CQRS Evolved - CQRS + Akka.NET
PPTX
Project Orleans - Actor Model framework
PPT
Devops at Netflix (re:Invent)
PPTX
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
KEY
Cooking a rabbit pie
PDF
Webinar: Queues with RabbitMQ - Lorna Mitchell
PPTX
Elm - Could this be the Future of Web Dev?
PPTX
Real world Scala hAkking NLJUG JFall 2011
Python Raster Function - Esri Developer Conference - 2015
The Actor Model - Towards Better Concurrency
Intro to elixir and phoenix
Kubernetes at NU.nl (Kubernetes meetup 2019-09-05)
CQRS Evolved - CQRS + Akka.NET
Project Orleans - Actor Model framework
Devops at Netflix (re:Invent)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Cooking a rabbit pie
Webinar: Queues with RabbitMQ - Lorna Mitchell
Elm - Could this be the Future of Web Dev?
Real world Scala hAkking NLJUG JFall 2011
Ad

Similar to Combining the Strengths or Erlang and Ruby (20)

PDF
At Scale With Style (Erlang User Conference 2012)
PDF
At Scale With Style
KEY
Introduction to Actor Model and Akka
PPTX
Evented Ruby VS Node.js
PDF
Large-scaled Deploy Over 100 Servers in 3 Minutes
PDF
TorqueBox at DC:JBUG - November 2011
KEY
Erjang - A journey into Erlang-land
ZIP
mtl_rubykaigi
PDF
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
KEY
TorqueBox - Ruby Hoedown 2011
PDF
Rocket Fuelled Cucumbers
PDF
Ansible - A 'crowd' introduction
PDF
Rails Application Optimization Techniques & Tools
PDF
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
PPTX
Introduction to ansible
PDF
Egearmand: an Erlang Gearman daemon
PDF
Rooster Tech Talk
PPTX
ansible-app-platforme-2024-presentation-
PPTX
Ansible presentation
PDF
Highly concurrent yet natural programming
At Scale With Style (Erlang User Conference 2012)
At Scale With Style
Introduction to Actor Model and Akka
Evented Ruby VS Node.js
Large-scaled Deploy Over 100 Servers in 3 Minutes
TorqueBox at DC:JBUG - November 2011
Erjang - A journey into Erlang-land
mtl_rubykaigi
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
TorqueBox - Ruby Hoedown 2011
Rocket Fuelled Cucumbers
Ansible - A 'crowd' introduction
Rails Application Optimization Techniques & Tools
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Introduction to ansible
Egearmand: an Erlang Gearman daemon
Rooster Tech Talk
ansible-app-platforme-2024-presentation-
Ansible presentation
Highly concurrent yet natural programming
Ad

More from Wooga (20)

PPTX
Story of Warlords: Bringing a turn-based strategy game to mobile
PDF
Instagram Celebrities: are they the new cats? - Targetsummit Berlin 2015
PDF
In it for the long haul - How Wooga boosts long-term retention
PDF
Leveling up in localization! - Susan Alma & Dario Quondamstefano
PDF
Evoloution of Ideas
PDF
Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
PDF
Saying No to the CEO: A Deep Look at Independent Teams - Adam Telfer
PDF
Innovation dank DevOps (DevOpsCon Berlin 2015)
PDF
Big Fish, small pond - strategies for surviving in a maturing market - Ed Biden
PDF
Review mining aps2014 berlin
PDF
Riak & Wooga_Geeek2Geeek Meetup2014 Berlin
PDF
Staying in the Game: Game localization practices for the mobile market
PDF
Startup Weekend_Makers and Games_Philipp Stelzer
PDF
DevOps goes Mobile (daho.am)
PDF
DevOps goes Mobile - Jax 2014 - Jesper Richter-Reichhelm
PDF
CodeFest 2014_Mobile Game Development
PDF
Jelly Splash: Puzzling your way to the top of the App Stores - GDC 2014
PDF
How to hire the best people for your startup-Gitta Blat-Head of People
PDF
Two Ann(e)s and one Julia_Wooga Lady Power from Berlin_SGA2014
PDF
Pocket Gamer Connects 2014_The Experience of Entering the Korean Market
Story of Warlords: Bringing a turn-based strategy game to mobile
Instagram Celebrities: are they the new cats? - Targetsummit Berlin 2015
In it for the long haul - How Wooga boosts long-term retention
Leveling up in localization! - Susan Alma & Dario Quondamstefano
Evoloution of Ideas
Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Saying No to the CEO: A Deep Look at Independent Teams - Adam Telfer
Innovation dank DevOps (DevOpsCon Berlin 2015)
Big Fish, small pond - strategies for surviving in a maturing market - Ed Biden
Review mining aps2014 berlin
Riak & Wooga_Geeek2Geeek Meetup2014 Berlin
Staying in the Game: Game localization practices for the mobile market
Startup Weekend_Makers and Games_Philipp Stelzer
DevOps goes Mobile (daho.am)
DevOps goes Mobile - Jax 2014 - Jesper Richter-Reichhelm
CodeFest 2014_Mobile Game Development
Jelly Splash: Puzzling your way to the top of the App Stores - GDC 2014
How to hire the best people for your startup-Gitta Blat-Head of People
Two Ann(e)s and one Julia_Wooga Lady Power from Berlin_SGA2014
Pocket Gamer Connects 2014_The Experience of Entering the Korean Market

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Review of recent advances in non-invasive hemoglobin estimation
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Weekly Chronicles - August'25 Week I

Combining the Strengths or Erlang and Ruby

  • 1. COMBINING  THE  STRENGTHS  OF   ERLANG  AND  RUBY erlounge  Berlin  February  2012  –  Mar4n  Rehfeld
  • 2. Game  Server  for  Upcoming  Wooga  Game What  is  a  game  server? • provide  HTTP  API  to  actual  game  („client“) • validate  API  calls  against  current  state  &  game  logic • API  calls  will  modify  the  game  state • make  game  state  persistent
  • 3. Game  Server  for  Upcoming  Wooga  Game It  will  be  a  stateful  game  server • one  process  per  ac4ve  user  gaming  session • the  process  holds  the  current  state  and  is  the  only  one  that  can  modify  it   (strong  encapsula:on) • the  process  handles  all  API  calls  for  the  given  user  one  a=er  the  other   (concurrency  control  through  actor  model) • the  process  loads  the  game  state  from  storage  and  writes  it  back   periodically  and  on  process  termina:on  (:meout  =  user  stopped  playing)
  • 4. Game  Server  for  Upcoming  Wooga  Game Details  on  the  basic  idea: Awesome  presenta4on  on  the Magic  Land  game  server  by @knu4n  &  @hungryblank hZp://www.slideshare.net/wooga/from-­‐0-­‐to-­‐1000000-­‐daily-­‐users-­‐with-­‐erlang
  • 5. Our  Goals • Get  most  of  the  benefits  from  wooga’s  pure-­‐erlang  game   server  (Magic  Land) • especially  func:onal  approach  for  game  state   encapsula:on  and  transforma:on  +  concurrency  control • But:  Keep  Object-­‐Oriented  approach  for  modelling  the   game  logic
  • 6. Expected  OO-­‐Benefits • Rapid  development • concise  &  expressive  syntax • leverage  exis4ng  know  how • but:  keep  the  OO  part  stateless  and  side-­‐effect  free to  avoid  the  usual  traps  &  pidalls
  • 7. Target  Architecture Authority for: Load Balancer "What app server is responsible for current session?" App Server Node App Server Node App Server Node Erlang VM Erlang VM Erlang VM ... Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby ... ... ... Worker Worker Worker Worker Worker Worker Worker Worker Worker Shared Storage for game state snapshots and for storing cold game sessions
  • 8. Example  Game  Ac4on  in  Ruby URL game_action '/:actor/fruit_tree/self/shake', :affects => [:fruit_trees, :user] do |response| x, y = params[:x], params[:y] fruit_trees[x, y].shake affected  parts  of end the  game  state • DSL-­‐like  defini4on  of  game  ac4on • skinny  as  controllers  should  be  8-­‐)
  • 9. Example  Model  in  Ruby Inheritance class FruitTree < Tree property :last_shake_time, :type => Integer, :default => 0 DSL-­‐like  defini4on property :collectable_fruit_count, :type => Integer, :default => 0 of  persistent  state def shake raise Error::Validation, "FruitTree at (#{x}, #{y}) has no fruit" unless carries_fruit? session.user.xp += 1 session.user.energy -= 1 self.last_shake_time = game_time self.collectable_fruit_count = config.fruit_count end # ... end • easily  unit  testable • minimal  amount  of  code
  • 10. erlang  talking  to  Ruby Some  op4ons • erlectricity  hZps://github.com/mojombo/erlectricity: Can  talk  erlang  binary  protocol  to  Ruby  processes  through  erlang   ports • ernie  hZps://github.com/mojombo/ernie: Remote  func4on  call  using  the  BERT-­‐RPC  protocol  to  either  Ruby  or   na4ve  erlang  processes • ZeroMQ  <hZp://www.zeromq.org/>: awesome  brokerless  queue  transport  layer,  connects  30+  languages
  • 11. ZeroMQ Pro Con • loose  coupling  of  erlang  and   • yet  another  layer Ruby  through  queues • easily  deploy  new  Ruby   code  without  touching   erlang • allows  flexible  transports,   even  accross  machines
  • 12. Connec4ng  the  Dots • use  Mongrel2  hZp://mongrel2.org/  protocol  &  ZeroMQ  setup • erlang:  emongrel2  hZps://github.com/hungryblank/emongrel2 • Ruby • rack-­‐mongrel2  fork  hEps://github.com/khiltd/khi-­‐rack-­‐mongrel2 • rack  protocol  hEp://rack.rubyforge.org • Sinatra  hEp://www.sinatrarb.com/ ➡ essen4ally  we  are  speaking  HTTP  over  ZeroMQ and  can  hook  up  any  Rack-­‐based  Ruby  web  framework
  • 13. Setup  Overview server worker session worker sender Push/Pull  Queue 1:n worker session worker ... receiver Pub/Sub  Queue m:n ... session worker
  • 14. How  does  the  game  state  look  like? • Has  many  parts,  each  part  has  a  name  (e.g.  fruit_trees)  and  some   content • this  is  a  performance  op:miza:on,  so  that  we  don't  need  to  send  the   complete  state  back  and  forth  for  every  call • erlang  does  not  care  about  the  content  (binary  data) • actually  the  content  contains  serialized  objects  (e.g.  a   MapObjectCollection  containing  FruitTree  members) • erlang  does  need  to  know,  what  game  ac4on  needs  what  state  parts
  • 15. Looking  back  at  the  Game  Ac4on game_action '/:actor/fruit_tree/self/shake', :affects => [:fruit_trees, :user] do |response| x, y = params[:x], params[:y] fruit_trees[x, y].shake affected  parts  of end the  game  state • Ruby  knows  the  mapping  of  game  ac4ons  to  affected  state  parts • pushes  the  mapping  on  startup  &  makes  it  available  on  request
  • 16. Aside:  Efficient  Game  State  Access • considered  different  serializa4on  formats  (e.g.  JSON,   MessagePack) • especially  for  large  collec4ons  of  objects  we  wanted  to  avoid   parsing  everything  to  extract  a  single  object • chose  TNetstrings  hZp://tnetstrings.org/  as  a  serializa4on   format  (length  prefix  helps  searching/lazy  parsing) • built  a  lazy  parser/encoder  for  it    in  C  for  speed  :-­‐) hZps://github.com/wooga/lazy_tnetstring
  • 17. Q  &  A Mar4n  Rehfeld @klickmich