Running A Popular Website On 512 MB RAM

The Results of Varnish + Redis + Memcached + WP Super Cache

Most hosting researchers and people who run websites might lead you to believe that in order to run a popular website, you need to spend a lot of money and it needs to be on a top notch server. However, from what I have seen, neither is really necessary, but it all lies in your actual setup of the server. Caching software will be your best friend when it comes to making receiving 10,000 visitors daily a possibility. You could probably receive over 100,000 visitors a day with the right caching software installed on your website.

For those of you who are not familiar with any of these terms at all, summing them up:
Memcached: Used to store and retrieve data from the database via caching common requests, often used with dynamic websites to speed up the process.
Redis: An in-memory key-value store (with disk-persistance) primarily used as a cache system for databases.
Varnish: works at the server level, considered a cache server which is a dedicated network server or service acting as a server that saves Web pages or other Internet content locally. Very few calls would have to be made to the database, speeding up the server and website.
WP Super Cache: WordPress plugin designed to cache pages in a folder as HTML and retrieved from the disk rather than the database when re-visited.
To do any of these four applications justice, a book could be written on all four of them if we wanted to get into the dynamics, perspectives, aspects, and technicalities of how they work, but for now: Just know that the first two have to do with the database and storing queries in memory (both are very similar, Redis may be more advanced, but Memcached is just as powerful), Varnish acts as a HTTP reverse proxy so when requested, all the work that is normally done by a website, such as Apache, NGINX, and PHP being called, server requests, server responses, are rarely being initiated, as the server quickly locates and calls up those locally stored files in its own cache (Read a more simplified term of Varnish explained), and WP Super Cache is a plugin that stores files locally on the web host. If I were to try and explain how they all work and work together, well, that is partially the reason for this post, but in a sense: it is magic to me. Though I know it is just code doing its job so naturally.

The whole purpose of these cache systems: store everything in memory or file rather than hitting the database. Querying to a database takes time, from server request and server response and client request and client response. If you can avoid the database and even retrieving it from the hard disk of the web server by storing it in memory, it will be retrieved much faster. If you have hundreds of visitors all visiting, the website is constantly sending queries to the database and must wait for it to respond. If those queries and pages are stored in memory, than your visitors do not have to wait as long for a web page to load.

For years, I had my websites on "optimized WordPress" servers and while I will say, if you want very little to worry about, and you can afford it, definitely go for it, but those types of servers can run you anywhere from $15 to $50, just so you can receive "unlimited" bandwidth and ensure that no matter how much traffic that your website gets, it is hardly ever going to go down. There are certainly advantages to some of these "optimized servers" which include their own caching systems, especially when they come with free SSL and Varnish, making them incredibly fast. By all means, do not let this post sway your decision to go with optimized web / WordPress hosting if you can afford it and it suits your needs. I definitely recommend DreamPress from DreamHost.

This post is for those who are looking to downgrade their instances or droplets in order to save a few bucks. Yes, its just a few bucks, but saving $20 - $100 a year can mean a lot for some of us.

For the purposes of this article, we will assume that you are using a system like DreamCompute, Vultr, or DigitalOcean or Linode for your server and have spun up a 512 MB or 1 GB instance on Ubuntu because you want control of your own server.

I run a semi-popular website known as Confessions of the Professions for which it began on Shared Hosting, than it got too popular and the host suggested that I upgrade for more resources. At the time, DreamHost was offering a new service in beta called DreamPress. I hopped on board, paying $16.95 a month and enjoyed it for 2 years. While it was well worth it, it tends to get expensive when you are strapped for cash some months, so I looked into their new service, known as DreamCompute. DreamCompute offers Cloud Hosting from $4.50 a month for 512 MB of RAM to $6.00 a month for 1 GB of RAM, $12 for 2 GB of RAM, up to $96 a month for their most expensive plan, at 8 GB of RAM. You could also sign up for Vultr and get a plan for $2.50 for 512 MB, but you are limited to just two instances. DreamCompute is one of very few services that offers unlimited (approximately) 32 512 MB instances.

Combine DreamCompute with DreamObjects, DreamHost's CDN and you have a very powerful website with just 512 MB. None of your media files are ever stored on your website, freeing up tons of bandwidth, and showing near-instant images with each page load. Both come at a very affordable pricing and are very complimentary of each other. A CDN may save just a few seconds of download time, but those seconds are worth every penny!

When I made the switch to DreamCompute, with the help of ServerPilot (read about setting up DreamCompute on ServerPilot), I immediately wanted to get on the cheapest plan, hopping on to 512 MB, only for it to keep crashing on me because the website generally receives around 30,000 to 50,000 visitors a month. The website uses WordPress and less than 30 plugins to help with daily operations. It kept crashing and so I upgraded to 1 GB of RAM. $6 a month is $72 a year. $4.50 a month is $54 a year. Very affordable. I installed WP Super Cache and the website ran just fine.

Over time, I kept reading about different cache methods and eventually installed memcached on the server. Before this was installed, the website would occasionally go down and it seemed to struggle, even at 1 GB. After installation, there was ZERO downtime. I was very impressed, but wondered if there was anymore I could do. I decided to look into the Redis Server, which caches the database at the server level. This has been the ultimate combination, caching object and database, and making the website incredibly fast.

I also decided to look into Varnish Cache, which, if you search for it, it is very rare for anyone to be talking about combining all three. Memcached + Redis + Varnish. While ServerPilot does not recommend Varnish cache, it can be installed. I managed to successfully install all three, but ran into some trouble. The website, isvarnishworking.com, would say, "It is working.. sort of", working only when I landed on a page of the website. Theoretically, all three working together would mean a blazing super fast website. Ideally, all three would work simultaneously and in sync with each other, performing flawlessly. Realistically, all three working together is not as easy as it seems. Maybe someone out there has gotten all three of them to work successfully, but from my own personal experience, it was as if they were competing for resources and the website was running very slow or crashing, specifically when trying to get into the backend of WordPress.

Thus, I ended up disabling and completely removing Varnish, but the other two... three work very well together. Perhaps a combination of Redis + Varnish or Memcached + Varnish will work, but for my own setup, Memcached + Redis + WP Super Cache seems to work in sync with each other, without issue. They actually seem to compliment each other, as pages are loading in about 2-3 milliseconds. So this is the perfect setup that seems to handle hundreds to thousands of visitors a day, all on a 512 MB RAM instance. 7-10 MB (1-2% of total RAM) with about 10% - 30% load on the CPU is most common, with average CPU load being around 10%.

I decided to run it through some Loader tests. 1,000 visitors over 1 minute was easily handled with no errors. I kept increasing the visitor count and was able to get up to 5,000 with no issues. The screenshot below shows that 5,000 client connections would be able visit website without any issues at all.
Unfortunately, the max seems to be around 5,000 visitors on 512 MB of memory. I attempted 7,000 and 10,000 but the error threshold was too high for the tests to continue. Fortunately, it is highly unlikely that unless you go viral overnight or become a website like Facebook or YouTube, you are unlikely to experience this issue. If you do manage to start getting over 5,000 visitors per minute, it would certainly be wise to upgrade to far greater than 2 GB of memory for your website. By then, it might be worth the investment. Until then, however, 512 MB is easily able to handle 5,000 visitors and if you are on WordPress, you can run at least 30 - 50 about 20-30 plugins without many issues. However, you will likely need to heavily cache your website so most of the hits are just a cache of the database and full page (Memcached + Redis Server) being served up to your visitors via HTML (WP Super Cache). If you only post once or twice a day, this is no issue. The RAM use is almost nothing.


Additionally, it is highly advised that you disable wp-cron.php, which gets called on every page load, so long as someone, any one person lands on any single page on your website, which can definitely be a lot of calls and use up resources. You should run a cron job for it instead. CPU load goes from a range of 10% - 40% to a range of 1% - 15%.

You can do this by adding this line of code to wp-config.php:

define('DISABLE_WP_CRON', true);
If you are on a VPS or a Cloud server, you can use Terminal and create a cron job by typing in these commands:
crontab -e -u USERNAME
Then just make sure you call the right file, using both the correct PHP version and the location of your wp-cron.php file in WordPress.
*/5 * * * * php7.x /html/users/USERNAME/apps/APPNAME/public/wp-cron.php
This is a cron job that will run every 5 minutes.

Can I run a popular website with just 512 MB of RAM? The answer is YES. You have to have the proper caching setup to manage it. You can certainly try it without caching, but you may experience crashing every so often, but it costs nothing but a few minutes of your time to install Memcached, Redis Server, and WP Super Cache on to your website.

Why would I want to run a website with just 512 MB of RAM? The simple answer: Because you can and it saves you money. If you can run your website on 512 MB of RAM which is usually the minimum amount of RAM required to run a WordPress website with some plugins, than you will save yourself a ton of money. There is also another upside to this and it works if you are in the business of hosting client websites. Imagine putting a client website on 512 MB of RAM and being able to charge them $10/month or more while you can easily profit the additional remainder. Most of your clients will likely never need more than 512 MB of RAM.

WARNING: If you are running a WordPress website and have these 3 methods of caching installed, you may need to disable WP Super Cache if you make changes to your plugins, specifically WordPress Jetpack. If you are worried about the 24 hour period of caching, than just set it to 12, 6, or even 4 hours before the next cache refresh.

CAUTION: Before you install ANY of these caching systems, ensure that you have made a backup of your database and files, as anything could go wrong at any time.

Note: In the beginning, as the cache systems need to start working on your website, you may experience some outages at first, and may even possibly need to hard reboot your server on several occasions. This will lead you to believe that it is not working and you may even consider switching to higher memory. My patience had almost run thin and I was nearly ready to upgrade the memory again, before I realized just how cached the website was and there weren't any emails about downtime at all. So you must have a little patience for the trio (Memcached+Redis+WP Super Cache) to do the job they were programmed to do. It will be likely that just every 24 hours (or whenever you have it set), the page will be called once from the database and after that, it is cached, but considering that every page is being cached throughout the day, it takes time to cache all pages and database queries. It took about 2-3 days for my website to fully be cached. If issues do persist, disable WP Super Cache, as this may just be some overcaching going on.

Update 9/3/2017: If you are running Memcached + Redis, you do not need WP Super Cache enabled.

Update: 9/9/2017: This setup at 512 MB is ideal if your website is mostly static or you update your blog only several times a week.