Scaling WordPresshttps://scalingwp.wordpress.com
The WordPress 'big projects' group. Practical advice, discussion and idea sharing for scaling WordPress.Fri, 09 Mar 2018 23:26:41 +0000enhourly1http://wordpress.com/https://s2.wp.com/i/buttonw-com.pngScaling WordPresshttps://scalingwp.wordpress.com
Scaling a WordPress membership site?https://scalingwp.wordpress.com/2016/03/18/scaling-a-wordpress-membership-site/
https://scalingwp.wordpress.com/2016/03/18/scaling-a-wordpress-membership-site/#commentsFri, 18 Mar 2016 11:57:05 +0000http://scalingwp.wordpress.com/2016/03/18/scaling-a-wordpress-membership-site/I have a more specific problem. I can cache content for non-registered users but I do not know how to scale WordPress for my membership area? What should I do to serve as many logged in users as possible concurrently? AFAIK caching is not recommended for logged in users. Then what do I do? I use Debian and Nginx.
]]>https://scalingwp.wordpress.com/2016/03/18/scaling-a-wordpress-membership-site/feed/2edudilAPC opcode caching of multiple siteshttps://scalingwp.wordpress.com/2012/08/27/apc-opcode-caching-of-multiple-sites/
https://scalingwp.wordpress.com/2012/08/27/apc-opcode-caching-of-multiple-sites/#commentsMon, 27 Aug 2012 23:52:45 +0000http://scalingwp.wordpress.com/?p=87APC performs two functions, one as an object cache and the other as an opcode cache. In this post I’m talking specifically about the opcode caching functionality when used on a server hosting multiple websites.

What is an opcode cache?

I’m sure most of you know but I’ll just cover it quickly for those that don’t. Each time a PHP web page is requested the web server has to compile the human readable PHP code into a language the processor can understand, this is called opcode. APC will cache opcode in RAM so that subsequent requests for that file do not have to go through that same process of opening up the file from disk and compiling it into opcode. So not only does it save on compiling the code it also saves the disk access. If your website’s files are located on a distributed filesystem such as NFS then opcode caching will give you upwards of 100% improvement in performance any day of the week.

Configure APC with enough memory for your needs

APC’s default configuration is probably fine if you’re hosting a single website with hardly any plugins and a basic theme. Otherwise you should change the configuration, especially If you have more than a one website hosted on the server or your website has lots of PHP files.

In its default state APC will allocate 30MB of shared memory. The PHP files of a pretty moderate WordPress website will need more than 30MB space for the opcode cache (there are a lot of PHP files). You’ll probably want to look at budgeting 40MB for each site (so 10 websites would wipe out the RAM of a 500MB small cloud server). If APC runs out of space to store its cached PHP files then it will totally expunge the cache and start over and if that happens on every page load then you can say goodbye to your performance increase. There are some settings you can tweak to improve things a little but really you just need to allocate enough memory to APC.

Use apc.php to keep track of your APC usage and make sure it has enough free space to fit everything in. If you use APC as an object cache then you will need to allocate even more space to every website you host.

The benefits of using the APC opcode cache on a WordPress website

The graph on the left is without APC enabled and the graph on the right has APC enabled. The initial load test done without the APC opcode cache nearly crashed the server at 75 clients so the load test had to be stopped. Once APC was enabled requests per second are pretty stable with the server now easily doubling its performance.

More bang for your buck

I’ve thought for some time now that if you run multiple sites on your web server and those sites use a common codebase then disk space, RAM and processor time could be saved by creating a symlink to those common files. Rather than each website loading up and caching its own copy of the same code it would be much better if that code could be cached once and accessible to all sites.

In my case the codebase is WordPress but this could also apply to other frameworks such as CodeIgniter or CakePHP.

So to test this and confirm my initial thoughts I created a single line PHP file called simon.php, then created 2 symlinks and 2 hard links to that PHP file. Then went through a process of clearing the APC system cache, executing the symlinks, checking with apc.php to see if there were entries in the system cache for either the links or the file they referenced.

No matter which of the 4 files I accessed in a web browser there was only a single entry in the APC system cache. Each time I refreshed a page the system cache hit count incremented by one, whichever of the 4 files I accessed that same counter increased. It’s the path which is initially used to access simon.php that is recorded by APC then any subsequent requests that ultimately resolve to the simon.php file get attributed to the same entry in APC.

There is one slight difference between using hard and soft links in this situation (besides the usual differences). If the first request to a PHP file is through a hard link then the path to the hard link is stored by APC. If however the first request to a PHP file is through a soft link (symlink) then it’s the ultimate path (simon.php) that is recorded.

So you should be able to share a collection of PHP files among an unlimited number of sites using soft/hard links and those files will only be put in the opcode cache once and therefore only be taking up space in RAM once.

Let’s just confirm at a lower level that the opcode is being cached

I still felt like I needed further proof, so I forced Apache to just use a single process and then I watched that process to see exactly what it was doing each time it processed a request for my links. I’ve included those interactions between Apache and the OS in the gists below, both when Apache should have been opcode caching the file and when it should have been reading the file out of the cache rather than compiling it all over.

For those of you that can’t be bothered to read through those gists (120-180 lines each) It pretty much goes like this:

Initial request to a PHP file using the hard link

Apache uses the “stat” command to get some information about the file requested

Apache tries to open .htaccess files all the way up the directory tree

Apache uses the “lstat” command to get information about the link and every directory leading to that link file (goes through this exact process 3 or 4 times for some reason)

Then it opens up the file from the disk

The file is then added to the opcode cache in shared memory

Serves the resulting file to the browser

Logs the request

Subsequent requests to a PHP file using the hard link

Apache uses the “stat” command to get some information about the file requested

Apache tries to open .htaccess files all the way up the directory tree

Apache reads the file from the opcode cache in shared memory

Serves the resulting file to the browser

Logs the request

The only difference I can see when doing the same with a soft link (symlink) is that once Apache has used “stat” to get file information it then follows the link and switches to using the ultimate file name/path for further “lstat” checks.

————————————–

So the opportunity to squeeze out further performance in this situation is there. I’m definitely on the lookout for the best way to run separate WordPress installs with a shared core codebase (Multitenancy)

]]>https://scalingwp.wordpress.com/2012/08/27/apc-opcode-caching-of-multiple-sites/feed/7simonthepimanapc-compareMaking WordPress Scale, On A Budgethttps://scalingwp.wordpress.com/2012/08/24/making-wordpress-scale-on-a-budget/
https://scalingwp.wordpress.com/2012/08/24/making-wordpress-scale-on-a-budget/#commentsFri, 24 Aug 2012 10:57:14 +0000http://scalingwp.wordpress.com/?p=76This is copied in from the interconnect/it post on scaling WordPress.

If you create great content, your WordPress site is going to get a lot of traffic. That’s a good thing! One of our clients has done just that, but we had a couple of problems – he’s become popular in general, bringing in, on busy days, over 10,000 visitors, many of whom look around the site. And worse, he’s also become popular on Twitter.

This means that when he tweets about an update he and his many followers create huge spikes in traffic. But there’s a an issue of cost – the site, Sniff Petrol, carries no advertising and is essentially a spare time project for the owner. And that means there isn’t thousands to be spent on its hosting. We needed to manage these spikes well, but keep the costs down. A bigger server, as offered by the hosting company, was not the answer. It was time to geek out.

Experiments

Running experiments is the only way to test what will improve your sites performance. Below are the admittedly rather technical findings. We hope you find them useful.

sniffpetrol.com is a WordPress based motoring and motorsport satire site. It is currently hosted on a Linode VPS (Virtual Private Server) [affiliate link] with 4 CPU cores running at 2.27GHz and 1GB of RAM. A LAMP (Linux Apache, MySQL and PHP) installation is used to serve the site.

This article outlines the problems we encountered when this site experienced a sudden spike in traffic as well as methods we have employed to make the site more responsive under heavy load, without having to resort to using a more expensive server. A brief guide to how we implemented our solution will also be given and Changes made to the server configuration settings for Apache, PHP and MySQL will also be outlined.

The Problem

When using the default configurations for Apache, PHP and MySQL and no server-side caching, we found that when load testing the site, load times increased sharply as the number of concurrent users passed the 250 mark. Also, system load reached such high levels that the server became completely unresponsive (sometimes to the point of needing a manual reboot) due to excessive disk “thrashing” caused by Apache rapidly swapping from RAM to disk in an attempt to free-up more RAM to serve additional clients.

The Solution

The PHP, Apache and MySQL configurations on the server were changed from the defaults and the APC (Alternative PHP Cache) caching module was installed. In order to make best use of the APC caching module, the W3 Total Cache WordPress plug-in (version 0.9.1.3) was installed on the site. A brief guide to installing APC and W3-Total-Cache and getting them to work together can be found in the next section.

So, why did we decide to use the APC caching PHP module, or any other method of server-side caching for that matter? The short answer is: Efficiency.

APC allows us to cache dynamically generated content. This cached content can then be sent to the client when a request for it is received, instead of wasting more server resources to regenerate it when nothing has changed. This considerably reduces the load on the server.

We also made use of Amazon’s Cloudfront CDN (Content Delivery Network) and S3 services to store and serve static content (theme files and images, for example) to further lighten the load on the server. Our main reasons for choosing Amazon’s CDN solution were the pay-as-you-go pricing structure and the low storage/data transfer costs. A table detailing the costs can be found here.

The W3 Total Cache plug-in allows you to configure the site to make use of Amazon’s S3 and Cloudfront services as a CDN from the WordPress dashboard. It takes care of uploading the theme files – and other static content and also takes care of URL rewriting for uploaded files automatically. Overall, we were very impressed by how intuitive the whole setup process was. One online guide we found useful when setting up the CDN can be found on the Freedom Target site.

Getting APC and W3-Total-Cache Up and Running

If you are using Ubuntu Server, installing the APC Caching module on your server is as simple as running the command below:

sudo apt-get install php-apc

You will then need to restart Apache when the installation process has finished. Ubuntu/Debian users can do this by issuing the following command:

sudo /etc/init.d/apache2 restart

The installation and configuration of the W3-Total-Cache plug-in is a little more involved.

Before you install the plug-in, you will need to make sure that you have the following Apache server modules installed and enabled:

expires

mime

deflate

headers

env

setenvif

rewrite

It’s best to obtain the latest stable version from the WordPress plug-in SVN repository and upload the files to your server manually, rather than using the installer integrated into WordPress.

The plug-in comes with quite comprehensive documentation in the form of a readme file. Other setup guides can also be found quite easily on the Web. One installation guide we found useful can be found here.

When you have everything installed and the W3-Total-Cache plug-in has been activated, you will have to configure it to use the APC Caching module on the server. To do this, select the General Settings option from the Performance menu in the WordPress Dashboard and, from the dropdown list next to each option (Page Cache, Minify, Database Cache and Object Cache) select the ‘Opcode: Alternative PHP Cache (APC)’ option. Make sure that the Enable checkbox is checked for each option, and then click the Save Changes button next to each option.

Server Configuration Changes

The changes made to the configurations for each component of the LAMP stack are outlined below:

Apache

The following changes were made to the ‘Prefork MPM’, ‘Worker MPM’ and ‘Event MPM’ sections of the apache.conf configuration file:

TheKeepAliveTimeout option was set to 3 seconds to minimise the amount of time each apache process sits idle waiting for the client to send a KeepAlive request.

The MaxClients option was set to 250 to allow for more concurrent users.

TheMaxRequestsPerChild option was set to 400 to both minimise the consumption of system resources by an individual server process and to allow resources (especially RAM) to be freed up quicker.

MySQL

The relevant lines for the MySQL configuration file can be found below:
[mysqld]

key_buffer = 16M

max_allowed_packet = 16M

thread_stack = 192K

thread_cache_size = 8

myisam-recover = BACKUP

query_cache_limit = 1M

query_cache_size = 16M

[isamchk]

key_buffer = 16M

PHP

To lessen the consumption of RAM by PHP scripts when under heavy load, the memory_limit option in php.ini was changed to 64MB.

Testing Method

The load testing service Load Impact was used to perform the load testing on the server.

For each test, we used a simulated load of 250-1000 simultaneous clients with each ‘client’ spending an average of 20 seconds viewing a page. We started the test with an initial load of 250 clients and then ramped up the number of clients by 250 each time, up-to the limit of 1000 simultaneous clients.

Test Results

The User Load time results using the amended Apache, MySQL and PHP configurations without using APC caching or a CDN are shown below.

User Load Time (No APC caching or CDN enabled)

Although the server did not become completely unresponsive, the load time increases considerably after 250 clients, with load times exceeding 10 seconds after approximately 350 clients. The bandwidth usage results for this test can be found in the graph below:

Bandwidth Usage (No APC caching or CDN enabled)

The maximum amount of bandwidth used in this test (approximately 33 Mbps) was considerably less than the 100Mbps the server was capable of transferring. Taking both the user load time and bandwidth usage results into account, it was apparent that the server was not yet performing as efficiently as it should be.

With the APC caching module used in conjunction with the W3-Total-Cache plug-in on the site, the reduction in load times was considerable, with user load times at 1000 clients being approximately 25 times faster as the graph below shows:

User Load Time (Using W3-Total-Cache plug-in with APC caching)

The bandwidth usage results for this test can be found in the graph below:

Bandwidth Usage (Using W3-Total-Cache plug-in with APC caching)

Although there is a considerable improvement in bandwidth usage up-to 750 clients, the bandwidth usage drops to around the same level (33Mbps) at 1000 clients as was seen during the first test. This is possibly a function of the VPS having to share its network interface with other websites and may even be due to a certain amount of bandwidth throttling at the hosts.

Switching to Content Delivery Networks

When static content was served from the Amazon CDN and APC caching was enabled from within the W3-Total-Cache plug-in, we found that performance could be further improved:

User Load Time (W3-Total-Cache Plugin and Amazon S3 CDN Used)

Although the performance is not as dramatic as the previous test, when compared to the load times with APC-caching only, the increase in load times as the number of concurrent clients increase is much smoother. The bandwidth usage graph for this test can be found below. The data shown is the combined bandwidth usage of both the server and the CDN:

Bandwidth Usage (W3-Total-Cache Plugin and Amazon S3 CDN Used)

Here, we found that the bandwidth usage increased far more smoothly as the test progressed, than in the test with APC caching and W3-Total-Cache only. This is to be expected, as the server no longer had to deal with serving large static files so fewer system resources were required to serve the same number of clients.

Conclusion

It’s easy to see that using server side caching and careful server configuration gives excellent results. What using a content delivery network means is that the delivery of content will grow more consistently. One problem with many servers, and one which is rarely acknowledged, is the performance available from the network interface. Most won’t serve more than 100mb/s in theory, and about 70mb/s. What can’t be seen in the charts is the momentary output peaks of over 130mb/s that we saw using the content delivery network. The charts just show the averages. As a consequence it’s hard to show the improvement gained from using a CDN at the 1000 user level.

What we’d like to do, in the future, is to test the server up to 5,000 concurrent users. This is serious traffic, and also costs quite a bit of money to test. At the moment we know that the Sniff Petrol site can handle around 130,000+ page views per hour. But it may be able to handle a lot more. We’d love to see how far it can be pushed. Would it be possible to have the capacity to serve up to a million pages in an hour without having to commission a massive server? Keep coming back as we’ll be carrying out this test in the future.

As most of our clients use their own large scale hosting (we work with newspapers and publishers a lot!) we’ve generally let them worry about hosting requirements. They usually do pretty well and have some impressive hardware. But recently we’ve started offering a managed WordPress hosting service to our clients, and had to start learning about WordPress scaling ourselves. We love efficiency, and the idea of simply buying bigger boxes as a solution to performance problems appalls us. Modern computers are incredibly powerful – they can do a lot, for very little money.

Total monthly cost aprox: £160[You could lower this cost further by loosing the Red Hat license fees]

We wrote a custom plugin to allow the media library to upload direct into Rackspace Cloud Files, improving loading times through CDN, and solving the problem of sharing files between servers We also serve as many of the static theme files as possible from the Rackspace CDN.

]]>https://scalingwp.wordpress.com/2012/08/24/current-setups/feed/3gavinpearce379Howdy all we did some research on scaling…https://scalingwp.wordpress.com/2012/08/24/howdy-all-we-did-some-research-on-scaling/
https://scalingwp.wordpress.com/2012/08/24/howdy-all-we-did-some-research-on-scaling/#commentsFri, 24 Aug 2012 10:18:09 +0000http://scalingwp.wordpress.com/2012/08/24/howdy-all-we-did-some-research-on-scaling/Howdy all – we did some research on scaling up on a budget, and the results are here: http://interconnectit.com/1254/make-wordpress-scale-on-a-budget/

Using the same approach we’ve been able to set up a site that tested up to 5,000 concurrent visitors but, once we undid some things that caused stability problems we pulled back to about 3,500. Currently the site has a fail-over server but we could put a load balancer in front and pretty much double the capacity very quickly. That sort of load means a client with a million page views a month ticks over on a cheap 8GB VPS with a typical load average of 0.6 on a four processor machine.

]]>https://scalingwp.wordpress.com/2012/08/24/howdy-all-we-did-some-research-on-scaling/feed/5davidcoveneyWordPress Multitenancy (shared codebase)https://scalingwp.wordpress.com/2012/08/23/wordpress-multitenancy/
https://scalingwp.wordpress.com/2012/08/23/wordpress-multitenancy/#commentsThu, 23 Aug 2012 10:40:12 +0000http://scalingwp.wordpress.com/?p=36Jason McCreary gives an overview of what “WordPress Multitenancy” is and the steps he took to achieve it, along with his current working solution.

This is relevant to servers hosting a number of WordPress installs. Having a shared core codebase (not necessarily using multisite) means it’s easier to detect changes in the codebase by having it version controlled. Also less shared memory will be needed by the APC opcode cache, giving you more memory for object caching etc.

]]>https://scalingwp.wordpress.com/2012/08/23/wordpress-multitenancy/feed/14simonthepimanWelcome to Automattic/WordPress.comhttps://scalingwp.wordpress.com/2012/08/23/welcome-to-automatticwordpress-com/
Thu, 23 Aug 2012 09:55:27 +0000http://scalingwp.wordpress.com/?p=40Welcome to the guys and girls from Automattic/WordPress.com who joined us late yesterday.

Hopefully their experience and input in scaling WP will prove invaluable as this group grows.

From there we can collaboratively add resources to the comments which can be incorporated into the page by the administrator.

]]>https://scalingwp.wordpress.com/2012/08/22/proposal-resource-page/feed/1dwainmConsiderationshttps://scalingwp.wordpress.com/2012/08/21/considerations/
https://scalingwp.wordpress.com/2012/08/21/considerations/#commentsTue, 21 Aug 2012 16:47:23 +0000http://scalingwp.wordpress.com/2012/08/21/considerations/Lets list out some things to consider when looking to scale and some possible ways to overcome them.

To start off the conversation here are a couple items from the mailing list discussion:

Uploaded media files (WP stores files in a local install dir that is not easily changable)