How Does Memcached Make WordPress Hosting Faster?
Our WordPress hosting platform incorporates many performance optimizations that guarantee content is delivered to your WordPress site’s visitors as quickly as possible. Among them is the Memcached instance we include with all shared and dedicated WordPress accounts. I’d like to take a look at what Memcached is and how it works to make your site faster.
First, think about what happens when a browser sends a page request to a WordPress site. When WordPress receives the request, a PHP script is executed. The job of the script is to ask the site’s MySQL database for the data it needs to build the page. It then constructs an HTML page by combining that data with a template.
Which content is included depends on several factors: who requested the page, what they requested, the context of the request, and so on. The same base URL can return pages with completely different content — that’s what makes WordPress dynamic.
But often, the content isn’t different. You’re looking at a post generated by WordPress. If someone else requests this post, they’ll see the same thing for the most part. If left to its own devices, WordPress follows the process I have just outlined for every request, even if the post it is about to serve is exactly the same as the post it served five minutes ago. It takes time to build a page dynamically, so recreating identical pages over and over is wasteful.
Caching is the solution. Caching just means that after data is used, it’s saved for a while in case it’s needed again. Caching is ubiquitous: a WordPress server caches at every level, from its processor and RAM to caching plugins like W3 Total Cache (also included in our WordPress accounts) and content distribution networks.
Memcached is one of the layers of caching we use to decrease the amount of time-consuming work your WordPress site has to do. Memcached is a key-value store that works just like a dictionary. When you want to find the meaning of a word, you look it up in a dictionary by turning to the right place in an alphabetical list of words. The word itself is the key. Next to the word is the definition that explains what the word means. The definition is the value.
Unlike a word dictionary, Memcached uses a URL as the key. The web page is the value. The first time a page is requested, it has to be generated by WordPress and sent to the user in the usual way, but it’s also stored in the Memcached instance. The next time a page is requested, Memcached looks in its store for a key that matches the URL. If it finds the key, it returns the associated value (the web page’s contents) immediately.
Because the page is stored in Memcached, it doesn’t have to be generated again. Sending a page from Memcached is much faster than generating it from scratch and the server has to do a lot less work. Memcached is just one of the many reasons our WordPress hosting accounts are so fast — you can see some of the other optimizations here.