title: What is the best way I can scale my nodejs app?
body: <h1>The basics</h1>
  
  <p>Right now a few of my friends and I are trying to develope a browser game made in nodejs. It's a multiplayer top-down shooter, and most of both the client-side and server-side code is in javascript. We have a good general direction that we'd like to go in, and we're having a lot of fun developing the game. One of our goals when making this game was to make it as hard as possible to cheat. Do do that, we have all of the game logic handled server-side. The client only sends their input the the server via web socket, and the server updates the client (also web socket) with what is happening in the game. Here's the start of our problem.</p>
  
  <p>All of the server side math is getting pretty hefty, and we're finding that we need to scale in some way to handle anything more than 10 players (we want to be able to host many more). At first we had figured that we could just scale vertically as we needed to, but since nodejs is single threaded, is can only take advantage of one core. This means that getting a beefier server won't help that problem. Our only solution is to scale horizontally.</p>
  
  <h3>Why we're asking here</h3>
  
  <p>We haven't been able to find any good examples of how to scale out a nodejs game. Our use case is pretty particular, and while we've done our best to do this by ourselves, we could really benefit from outside opinions and advice</p>
  
  <h1>Details</h1>
  
  <p>We've already put a LOT of thought into how to solve this problem. We've been working on it for over a week. Here's what we have put together so far:</p>
  
  <h2>Four types of servers</h2>
  
  <p>We're splitting tasks into 4 different 'types' of servers. Each one will have a specific task it completes.</p>
  
  <h3>The proxy server</h3>
  
  <p>The proxy server would sit at the front of the entire stack, and be the only server directly accessible from the internet (there could potentially be more of these). It would have haproxy on it, and it would route all connections to the web servers. We chose haproxy because of its rich feature set, reliability, and nearly unbeatable speed. </p>
  
  <h3>The web server</h3>
  
  <p>The web server would receive the web-requests, and serve all web-pages. They would also handle lobby creation/management and game creation/management. To do this, they would tell the game servers what lobbies it has, what users are in that lobby, and info about the game they're going to play. The web servers would then update the game servers about user input, and the game server would update the web servers (who would then update the clients) of what's happening in the game. The web servers would use TCP sockets to communicate with the game servers about any type of management, and they would use UDP sockets when communicating about game updates. This would all be done with nodejs.</p>
  
  <h3>The game server</h3>
  
  <p>The game server would handle all the game math and variable updates about the game. The game servers also communicate with the db servers to record cool stats about players in game. This would be done with nodejs.</p>
  
  <h3>The db server</h3>
  
  <p>The db server would host the database. This part actually turned out to be the easiest since we found <a href="http://rethinkdb.com/">rethinkdb</a>, the coolest db ever. This scales easily, and oddly enough, turned out to be the easiest part of scaling our application.</p>
  
  <h2>Some other details</h2>
  
  <p>If you're having trouble getting your head around our whole getup, <a href="https://docs.google.com/drawings/d/1CfCOL60yZca8RQ9M1MCKRYNIMQDCnxX-P1pbYA2RcNI/edit">look at this</a>, it's a semi-accurate chart of how we think we'll scale.</p>
  
  <p>If you're just curious, or think it might be helpful to look at our game, it's currently hosted in it's un-scaled state here.</p>
  
  <h2>Some things we don't want</h2>
  
  <ul>
  <li>We don't want to use the cluster module of nodejs. It isn't stable (said <a href="http://nodejs.org/api/cluster.html">here</a>), and it doesn't scale to other servers, only other processors. We'd like to just take the leap to horizontal scaling.</li>
  </ul>
  
  <h2>Our question, summed up</h2>
  
  <p>We hope we're going in the right direction, and we've done our homework, but we're not certain. We could certainly take a few tips on how to do this the right way.</p>
  
  <h1>Thanks</h1>
  
  <p>I realize that this is a pretty long question, and making a well thought out answer will not be easy, but I would really appreciate it.</p>
  
  <p>Thanks!!</p>