How To: Supercharge your server performance by running your website ALL in memory!
Sometimes when your website receives an unexpected flow of traffic, it’s a great feeling of joy but it can also cause virtual indigestion to servers that aren’t able to keep up with the demand. For this reason, numerous caching methods are available to serve dynamic pages as static only content so the load relieves all stress from the database and application servers. However, since all files get served as static files, you’re now putting all the stress on your file system and hard drives.
Depending on the i/o and TPS capability of your storage devices, it could determine whether your server can withstand the load. If i/o is not able to withstand, you’d then start experiencing CPU performance degrade and system load averages hike up. The result is a system hang and unresponsiveness and most importantly loss of all that nice traffic surge you were expecting.
So how do you get around these issues? There are several ways and usually people just tend to scale up their hardware resources horizontally by adding more servers. This is obviously an expensive solution for a short-term so you should try all caching and software based alternatives rather than jumping to hardware solutions.
Here is a method that has helped us greatly in the past. Say you have a server that has 16GB of physical memory and your primary website size is around 2GB total. You can create a TMPFS and mount it to be perceived as local files system and copy all your website content to it, then make adjustments in your web config to point to the new docroot.
To create a file system with 2GB borrowed from physical memory:
# mkdir /www/mywebsite.com
# mount -t tmpfs -o size=2G,nr_inodes=10k,mode=0775,noatime,nodiratime tmpfs /www/mywebsite.com
# rsync -v -a /home/myoldwebsitepath/mywebsite.com/ /www/mywebsite.com
# service httpd restart
Now your entire website will run from memory and you should notice considerable boost in performance and system load dropping like a rock and staying down. This method only works until your system gets rebooted and then you’d have to follow the above steps once again. To automate, simply copy and paste the above lines into a script.