7. Some Performance Notes on Theming Your Drupal . any Site When theming your Drupal site, follow the KISS principle The fewer images, css rules, and especially Javascript make a big difference. If you use a lot of small images, try spriting
So why does it matter, why should I speed up my site?Google conducted a study to determine if speed would effect users. They took a sample of Google users and split it into 3 groups. A Control group was given the normal speed results when searching. A Second group they added 200 millisecond delay to the page load time, and a third group was given a 400 millisecond delay.200 milliseconds is 2 tenths of a second, and 400 milliseconds is 4 tenths of a second, both of these groups were less then half a second delay.What Google found, is that relative to the people in the control group, people who were given the less then half a second delay did less searches. The people who had the 200 ms delay did an average of .3% less searches over the 5 weeks, and people who had a 400 ms delay did .6% fewer searches.Now both of these numbers are well below 1% so you may not at first glance care, but this is a delay of less then half a second, If you have 3 or 4 second delays in your site you will see an even steeper decline.What’s more, Google’s research showed that the longer you are exposed to these delays, the fewer searches you do on average. [SHOW GRAPH] The black line is 200 ms delay, the red line is 400 ms delay.[CLICK]Speeding up the site itself
Before we dig into things you can ADD to Drupal to make it faster, let’s just take a moment to review how you can make Core Drupal run faster. It’s called the Performance settings, which you can find in your Admin panel under Configuration. [CLICK] This is the panel on Drupal 6, and [CLICK] here it is for Drupal 7. They’ve “optimized” the optimization settings. It’s much simpler in Drupal 7, but the core is the same. You can turn page caching on, block caching on, set a minimum lifetime of the cache, and optimze your CSS and Javascript by combining them together. I recommend turning all of this on. The one thing you should look out for, is in Drupal 6 you may have problems if you set the Caching mode to Aggressive, so I recommend Normal unless you really want to dig down and make sure everything still works.
Now that the core change is out of the way, let’s go beyond the Core. Here are some modules that you can [CLICK] Download from drupal.org to help get a drastic speed boost.The first, and perhaps most pronounced, is [CLICK] Boost. The boost module adds a caching layer beyond the built in drupal cache. Once your page has loaded, Boost saves a copy of the file in a folder on your server. It then compresses that file as a second copy. The next time someone needs to access the page, apache will serve the copy, and if the browser supports it, will serve the compressed copy to make it even faster. This means that for most page loads of your site, the site will require no database connections at all.On the previous screen we talked about Optimizing CSS and Javascript using the built in CSS and Javascript aggregators. You can make them even faster if those CSS and Javascript files are compressed, that’s where the [CLICK] CSS Gzip and [CLICK] Javascript Aggregator modules come in. These modules will Gzip compress those combined Javascript and CSS files, and the Javascript Aggregator will also “minify” the Javascript before Gzipping to make the file even smaller (and thus faster to load)Before I go on to a few more advanced modules, I want to mention a pair of [CLICK] Core modules that come with Drupal that you can enable to help with speed issues.The first is [CLICK] Syslog. Drupal’s logging system is great, but it also requires at least one database write for each and every page load which can cause speed issues. IF you don’t care about using Drupal’s watchdog log, but you still want logs you can go through, I highly recommend turning on the Syslog module and disabling the Database logging module. The same logging information will be logged to your system (or another system’s if you choose) syslog which you can then look at just like the rest of your server logs.Also, if your problems are that you are getting a few bad apples spoiling your speed for everyone else, the [CLICK] Throttle module can help you set limits on single users using up all your resources. Be very careful with this module however, because there are some users you really shouldn’t throttle, like Search Engine spiders trying to index your site. And if you set the Throttle rules too high, you will actually make your site appear SLOWER to your users, which is not a good thing.Here are some more modules you can download from drupal.org that will help you with speed, [CLICK] but will require external servers or server daemons on your server, so they are things that you may or may not be able to do in every situation[CLICK] Memcache is a great alternative to drupal’s built in database cache, and is fairly simple to install. Memcached the daemon can be installed fairly simply and replacing drupal’s database cache with memcache is as simple as adding a few lines to your settings.php in your drupal configuration. Memcache is an example of a NoSQL Database that is designed to be really fast because it operates entirely in RAM, it has no disk lookups, and since it only has a simple key => value store, it’s much easier to index, making it faster to look up information then a database. It’s downside is, because it operates entirely in RAM, it is very volatile, if the memcache server crashes, everything stored in it is lost as well. To get around this, there is a new software called Membase that can be a drop in replacement for memcache, cause it uses the same APIs to communicate with it. Membase is a more robust system that can store a persistent version of itself on a drive so if it crashes and is relaunched, it can reload itself from disk and you lose nothing. Also membase has replication and clustering built in, so when you start running out of membase space, you can launch another server and add it to the membase cluster and increase your cache pool.[CLICK] Perhaps the ultimate speed demon in Drupal is Varnish. Varnish is a proxy server designed to be the opposite of a proxy you use for browsing the internet. Varnish sits in front of your website, and caches whole pages in active memory, like memcache except the entire page is accelerated. Think of it as a more powerful combination of Boost and Memcache. And there’s a Varnish module that you can install in Drupal that will manage the Varnish cache for you. Now to get full use out of Varnish, you need to either be using Drupal 7, or there is a fork of Drupal 6 with a ton of speed optimizations built right in called [CLICK] Pressflow. If you are creating a new drupal site from scratch and you want to have a ton of optimizations built right in, including some impressive core hacks to fix things like Drupal’s annoying sessions, Pressflow is a fantastic place to start.Finally on the modules side, Caching is great, but you are always going to have page requests that are dynamic. Especially if your site is primarily used in a logged-in fashion, as caching is mostly designed for people who are logged out.
So why does it matter, why should I speed up my site?Google conducted a study to determine if speed would effect users. They took a sample of Google users and split it into 3 groups. A Control group was given the normal speed results when searching. A Second group they added 200 millisecond delay to the page load time, and a third group was given a 400 millisecond delay.200 milliseconds is 2 tenths of a second, and 400 milliseconds is 4 tenths of a second, both of these groups were less then half a second delay.What Google found, is that relative to the people in the control group, people who were given the less then half a second delay did less searches. The people who had the 200 ms delay did an average of .3% less searches over the 5 weeks, and people who had a 400 ms delay did .6% fewer searches.Now both of these numbers are well below 1% so you may not at first glance care, but this is a delay of less then half a second, If you have 3 or 4 second delays in your site you will see an even steeper decline.What’s more, Google’s research showed that the longer you are exposed to these delays, the fewer searches you do on average. [SHOW GRAPH] The black line is 200 ms delay, the red line is 400 ms delay.[CLICK]Speeding up the site itself
Now that the core change is out of the way, let’s go beyond the Core. Here are some modules that you can [CLICK] Download from drupal.org to help get a drastic speed boost.The first, and perhaps most pronounced, is [CLICK] Boost. The boost module adds a caching layer beyond the built in drupal cache. Once your page has loaded, Boost saves a copy of the file in a folder on your server. It then compresses that file as a second copy. The next time someone needs to access the page, apache will serve the copy, and if the browser supports it, will serve the compressed copy to make it even faster. This means that for most page loads of your site, the site will require no database connections at all.On the previous screen we talked about Optimizing CSS and Javascript using the built in CSS and Javascript aggregators. You can make them even faster if those CSS and Javascript files are compressed, that’s where the [CLICK] CSS Gzip and [CLICK] Javascript Aggregator modules come in. These modules will Gzip compress those combined Javascript and CSS files, and the Javascript Aggregator will also “minify” the Javascript before Gzipping to make the file even smaller (and thus faster to load)Before I go on to a few more advanced modules, I want to mention a pair of [CLICK] Core modules that come with Drupal that you can enable to help with speed issues.The first is [CLICK] Syslog. Drupal’s logging system is great, but it also requires at least one database write for each and every page load which can cause speed issues. IF you don’t care about using Drupal’s watchdog log, but you still want logs you can go through, I highly recommend turning on the Syslog module and disabling the Database logging module. The same logging information will be logged to your system (or another system’s if you choose) syslog which you can then look at just like the rest of your server logs.Also, if your problems are that you are getting a few bad apples spoiling your speed for everyone else, the [CLICK] Throttle module can help you set limits on single users using up all your resources. Be very careful with this module however, because there are some users you really shouldn’t throttle, like Search Engine spiders trying to index your site. And if you set the Throttle rules too high, you will actually make your site appear SLOWER to your users, which is not a good thing.Here are some more modules you can download from drupal.org that will help you with speed, [CLICK] but will require external servers or server daemons on your server, so they are things that you may or may not be able to do in every situation[CLICK] Memcache is a great alternative to drupal’s built in database cache, and is fairly simple to install. Memcached the daemon can be installed fairly simply and replacing drupal’s database cache with memcache is as simple as adding a few lines to your settings.php in your drupal configuration. Memcache is an example of a NoSQL Database that is designed to be really fast because it operates entirely in RAM, it has no disk lookups, and since it only has a simple key => value store, it’s much easier to index, making it faster to look up information then a database. It’s downside is, because it operates entirely in RAM, it is very volatile, if the memcache server crashes, everything stored in it is lost as well. To get around this, there is a new software called Membase that can be a drop in replacement for memcache, cause it uses the same APIs to communicate with it. Membase is a more robust system that can store a persistent version of itself on a drive so if it crashes and is relaunched, it can reload itself from disk and you lose nothing. Also membase has replication and clustering built in, so when you start running out of membase space, you can launch another server and add it to the membase cluster and increase your cache pool.[CLICK] Perhaps the ultimate speed demon in Drupal is Varnish. Varnish is a proxy server designed to be the opposite of a proxy you use for browsing the internet. Varnish sits in front of your website, and caches whole pages in active memory, like memcache except the entire page is accelerated. Think of it as a more powerful combination of Boost and Memcache. And there’s a Varnish module that you can install in Drupal that will manage the Varnish cache for you. Now to get full use out of Varnish, you need to either be using Drupal 7, or there is a fork of Drupal 6 with a ton of speed optimizations built right in called [CLICK] Pressflow. If you are creating a new drupal site from scratch and you want to have a ton of optimizations built right in, including some impressive core hacks to fix things like Drupal’s annoying sessions, Pressflow is a fantastic place to start.Finally on the modules side, Caching is great, but you are always going to have page requests that are dynamic. Especially if your site is primarily used in a logged-in fashion, as caching is mostly designed for people who are logged out.