Saturday, 19 October 2013

Fixing slow performance on mod_wsgi

I've just spent a couple of hours investigating why one of my sites has been responding so slowly, to the point where the monitoring software would report that the site was down - after a bit of reading and racking my memory I realised that in the past I had limited the number of threads and processes WSGI could use to serve the site.

I'm using Flask and mod_wsgi on Webfaction and so my config was under:
~/webapps/[app_name]/apache/conf/httpd.conf
In my effort to save memory, back when WebFaction only offered 256MB of RAM, I'd gone through my sites and set the threads and processes to "2" for the wsgi process:
WSGIDaemonProcess app_name processes=2 python-path=... threads=2
I'd also set MaxSpareThreads 2 (down from the default 3) and ThreadsPerChild 2.

After checking performance with serverstatus and checking memory usage with this script, I settled on
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
ThreadsPerChild 16
WSGIDaemonProcess ... processes=8 threads=16
Which took my response time down from around 15 seconds (which even with 2 threads and processes seems slow) to around 2 seconds from cold and well under a second when warm (the second check after restarting apache).  Much better!

Memory usage seems to be around 22MB per process, but with 512MB of RAM to play with, there's plenty of headroom.

No comments:

Post a Comment