Sunday, March 13, 2016

Caching in WordPress

Namecheap.com

I had a pretty rough night last month when I have to deal with an error message saying that WordPress could not connect to the database.

I logged into my server’s control panel and noticed that the server load was unfathomably high, much more than it could ever take. I’d been dealing with a weird CPU issue for a while so I restarted the server, expecting it to correct itself.
But when my server eventually restarted, the site came back but only for a second, it quickly went down again. Whatever was causing it wasn’t just a temporary issue.
I contacted my host, which told me that they were seeing very high levels of traffic to the server, more than it could handle. It turns out the article I had written about a recent plagiarism case on Reddit was getting some attention both via Reddit itself and Google searches. The volume just seemed too high.
But then I looked at the sample level of traffic that I managed to snag when the site came back up briefly. It was high, many times my normal level, but nothing the server shouldn’t be able to take easily. It had, in the past, handled spiked much bigger than this.
My host agreed and we worked together to keep the site offline but give me access. Once in, I realized that I had made a terrible mistake.
The week before, I had to, in an emergency, create a new theme for my site. As part of that I had disabled W3 Total Cache. While a great move at the time, when I was done I had forgotten to reenable it and the site was without any kind of caching.
I reenabled the plugin, checked that it was working and then opened the site back up to the rest of the world. Sure enough, though the load was high and the server was straining some, it was nowhere near buckling. The highly-elevated traffic remained for several days and, through it all, there wasn’t as much as a glitch or a hiccup.
If I had remembered to reenable W3 Total Cache, or any caching plugin, I probably wouldn’t have noticed the traffic spike until I checked the stats the next day and I certainly wouldn’t have people on Reddit commenting about how quickly my site went down.
It was an embarrassment that ended up being minor, but it serves as a reminder to every WordPress user: Make sure you are using a caching plugin.

Why Caching is Important

Most people don’t think about what happens behind the scenes every time someone visits a page, but your server does.
If you use WordPress, or any other similar content management system that doesn’t have caching, there are a series of steps that your server has to complete to go from page requested to page delivered.
Basically, without caching, when a visitor tried to load a page, the page itself doesn’t exist. Instead, the server receives the request, WordPress queries the database for the content that’s supposed to be in the page and builds the page on the fly before delivery. Once that visitor is gone or goes to another page, the page disappears and the process starts over again.
While this is an over-simplification, the main issue is that the server has to create the page every time someone looks at it. While this is great in that rapidly-changing pages are updated for each visitor, it’s a lot of work for a server and even a decent VPS, such as my own, can struggle under that if there are a lot of visitors coming at once.
A caching plugin, ont he other hand, keeps WordPress from having to create a new page every time. The page is created once and held onto (or cached) for a period of time, usually a few hours. Subsequent visitors to that page get the cached version, which prevents the server from having to check the database and assemble the page from scratch.
To the visitor, this is much faster and to the server, this is much easier.
To use a personal computer example. Imagine, if instead of using copy and paste, you had to retype a paragraph into a new document every time you wanted to repeat the text. Though retyping it lets you make changes to it as you go, copying and pasting is much easier and faster for you.
But as great as caching is, it’s something that’s frightfully easy to forget and the reason is very simple.

Why Caching is Easy to Forget

A lot of bloggers don’t see the need or importance of caching and it’s easy to see why. Caching is most useful when a lot of people are trying to load the same content. Most blogs don’t see the kind of traffic level where caching has a major impact one way or another.
For example, if a page your site only gets a visitor once or twice a day, then it probably is being loaded dynamically every visit, caching or no. The reason is that the cache, most likely, expires between visitors, forcing the creation of a new page.
On low-to-medium traffic sites, there are probably only a few pages that get enough traffic to regularly deliver cached content and, even then, it’s probably doesn’t adversely impact the server. A well-optimized server can serve a decent number of dynamic pages without a problem.
So while caching probably has at least some speed benefit, it’s usually one that’s difficult to notice, especially to the naked eye.
This prompts many bloggers to skip on caching or, in my case, forget to check if it’s working, because everything functions fine without it.
However, as my case showed, it can be a road to disaster.

When Caching Matters

One of the strange things about the Internet is that it has a way of quickly giving a lot of attention to something that had very little of it previously. Between social media such as Facebook and Twitter as well as social news sites like Reddit, a tidal wave of traffic can hit any site at any time.
These types of moments are what bloggers work for, an opportunity to reach a large audience and a chance to capitalize and maybe grow your reach long-term. However, you can’t do that if your site is down.
But it doesn’t make sense to buy and pay for hosting strong enough to survive those waves when 99% of the time you will only need a fraction of it. It’s like buying a huge bathtub to hold a glass of water, expensive and wasteful.
Instead, the best thing you can do is make sure your site is as efficient as possible so it can survive waves of traffic visiting the same page without trouble. Caching is the best way to do that.
So, if you don’t cache, you’re severely limiting the number of visitors your site can receive. While that may be fine most of the time, it basically shuts the door in the face of potential visitors who are coming to see you at your most popular.

Bottom Line

In the end, WordPress is a great CMS for many sites but its lack of caching means that it can easily overpower even decent servers quickly. Fortunately, a decent caching system is never more than a plugin away.
Truth be told, my reminder was not a particularly painful one, it could have been much worse. An hour or sporadic downtime could have turned into much longer since the site wasn’t “down” in a sense that my monitors would detect. It could have gone until the next morning before returning to normal.
However, I’m not going to let dumb luck be what helps me get my site back up. I’m going to make sure to always run a caching program and, though I’m certain I’ll have to disable it from time to time to do work, I’m going to make double sure to reenable it when done.
I don’t want to risk another Reddit wave shutting me down…
Namecheap.com
Load disqus comments

0 comments