开发者

Django--Killing my Server with Inefficient Code Or Bad Apache SetUp?

I was benchmarking my production server (it's in Beta) and the results were poor to say the least. On pages without any dynamic content, 1000 Requests with a concurrency of 1 returned 73 Requests/Sec.

When I start to add MYSQL queries to the equation, things quickly spiral out of control. The same 1000 requests on my homepage produce the following results:

CPU spikes to 50% Load spikes to 3.7 (though that doesn't always happen)

complete request:1000
failed requests:0
write errors:0
requests/sec: 2.44
transfer rate: 113.26[Kbytes/sec]

90% of requests are served within 142ms.  
95% of requests are served within 3531ms (it just keeps getting worse after that). 

Taking a look at top while I run the benchmark

  mysqld runs as a process is consuming roughly 7% of memory and 2.5% cpu
  Apache seems to spawn 7 concurrent processes at times
  At other points, Apache does not show up in Top 

I'm running preforked Apache on a Micro AWS instance (ubuntu) and I'll upgrade to a higher instance, but I worry that there is an underlying problem here with the code or my Apache setup.

I am deploying Django with Mod_WSGI and I set KeepAliveTimeout to 3 just in case a couple of slow processes were screwing me up.

My code for the homepage is seemingly straightforward and though it requires joins.

    def index(request):
         posts=Post.objects.filter(photo__isnull=False).order_by('date').开发者_Go百科distinct()[0:7]  
         ohouses=Open_House.objects.filter(post__photo__isnull=False).order_by('day').distinct()[0:4]

         return render_to_response("index.html", {'posts':posts,'ohouses':ohouses},context_instance=RequestContext(request))    

I have left the default configuration in place for MYSQL.

Could this all be attributable to running a Micro Instance? Could my instance be somewhat corrupted? Any other plausible explanations?


There's a ton that goes into quick response times. Django is pretty optimized for what it is, but relying on a framework alone will never get you where you want to be.

If you're going to use Apache, use the MPM fork, and even then disable all modules you don't absolutely need. Apache can be made to run fast, but it's not the fastest horse out there. You'll do better with something like Nginx or (cringe) Cherokee. Cherokee is a good webserver, but usability index is like zero.

Any static resources should be served directly by your webserver or better yet, off a CDN.

Assuming you've optimized your own code to not make inefficient use of queries, Django's built in, automatic query caching will help reduce the overall amount of queries needed to the database. After that, you need to employ something like memcached.

Then, there's the server itself. Depending on the size of your site, you may not need much RAM and CPU, but it's always better to have too much than not enough. It might be beneficial to put some artificial load on your server (automated testing, spidering your site, etc), and see how your system resources hold up. If you get anywhere near capping out (I'd say over 50% with simple tests like that), you need to add some more into your instance's pool.

Search online for articles on how to optimize MySQL. Out of the box, it tends to use a lot more resources than it actually needs; there's lots of room for improvement there. And, if it's not already on its own server, consider strongly offloading it to it's own server. If you're anticipating a lot of traffic, the same server responding to web requests and fetching data from a database will become a bottleneck quick.


Could this all be attributable to running a Micro Instance?

Micro instances burst to 2 CPUs for a short period of time, after which they are severely capped for several minutes. I wouldn't trust any benchmarks done on a Micro EC2 instance for that reason.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜