Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Background

I'm building a game at the moment that monitors external actions (tweets, FB likes etc.) and updates users at regular intervals with this updated information (every 10 seconds or so via. AJAX). At a set time, all users will be dropped into Google Streetview and they need to run around finding markers. The idea of the game is the markers are hidden until the user is near them, and because of this, we can't store these in JS (too easy to view in source). So, we've got an AJAX request sending off the lat/long every time the user moves so we can show the marker if they're in the right area.

Problem

We've got a few hundred people playing this game at any one time, and expecting more. Response times are around ~10/20ms, and we're making use of APC, but when we're hitting a few hundred users, the server response time is slowing considerably (especially when these people are running around streetview, and making a request or two, each, every second).

Question

We're on a LAMP stack, with 4GB of RAM (we can change this as we're on a cloud), but I was wondering if there's a better, more scalable way of handling this. I've been reading a lot about Nginx, so would that be a suitable addition to our architecture for handing more requests simultaneously? How do others handle this type of polling using the LAMP stack?

share|improve this question
add comment

1 Answer

Broadly there are two views to handling more requests, and serving faster. One view is to write extremely efficient code. The other view is to be have extremely efficient serving (server). You can try for best of both, but it is tough to achieve. Most of the sites are located somewhere between these two extremes. For both these approaches I can recommend switching over to nginx completely, if not for only serving your static files, as nginx outperforms apache in both these respects by an order of magnitude. nginx is non blocking server and doesn't have the overhead of php for serving non php files unlike apache.

New programmers and hobbyists tend to end up developing with pre-made frameworks, and platforms. These codes are not optimum as they are intended to serve a large audience. This group of people takes the view of optimizing their serving (servers) as they can not leave their framework and hence can not fully optimize their code. In this approach, in context of nginx on linux you can tune your server to squeeze the maximum performance you can from it:

  1. You would want to optimize your tcp stack that comes on your os.
  2. Increase the number of open file descriptors.
  3. You might want to place your www directory on a ramdisk.

  4. You would want to increase worker connections and processes in nginx and the number of workers if you decide to use nginx.

  5. You would probably want to set up caching to compensate for badly written code.
  6. You are currently using APC which is an object cache, you might want to set up page caching using APC too, which will again offer improvements by an order of magnitude.
  7. You might want to memcache your database queries which should provide space to database bottlenecks.
  8. Use larger buffers in your web and database server.

The other group, generally experienced programmers who write their own code from ground up, or can manage to do so at a later point in time, try to write super efficient code, and be minimalistic in their 'design' efforts to server lean http responses which performs optimally on any stack. I have no tips to offer here but merely to suggest to follow best practices from authorities in the respective domain. Though, I can tell you Facebook's story in this regard.

Facebook's is an iconic tale of progress from server optimization to code optimization. Originally built on PHP with apache, they soon ran into performance issues, at which point in time they started optimizing their servers heavily. The OS, PHP stack everything was optimized. Soon they had to start using memcache, which was quite recent from my memory. But no sooner they started experiencing service disruptions again. At which point, since they could'nt optimize PHP itself any further, they actually compiled it into machine code! Which is the end of line as far as improvements are concerned. Now they can only add more servers. Unless they switch their language. Which will also stop yielding benefits at a point.

So these are the two options ahead of you. Choose wisely!

Finally I can add that you should follow the handy set of rules and guidelines at Yahoo Yslow and Google Pagespeed for improving performance. Minify your JS, HTML, PHP, CSS, and place at top and bottom accordingly. Use a CDN. Design lighter pages etc. Best of luck!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.