Memcached is one of the caching mechanisms that reside on your hosting server. It mainly deals with the database queries that help in reducing the database load resulting in a fast loading web page. If your website/store relies heavily on database queries, using Memcached for WordPress website would significantly improve the performance and reduce the page load time.
The internet giants including YouTube, Reddit, Facebook, Twitter, and Wikipedia are using Memcached to boost the page load time. Google App Engine, Microsoft Azure, IBM Bluemix and Amazon Web Services also offer Memcached service through an API.
Considering its importance in boosting page load time, we (Cloudways) offer pre-installed Memcached on our managed WordPress hosting cloud servers. However, sometimes you may need to configure your application (WordPress) to take full advantage of Memcached.
Table of Contents
What is Memcached?
According to the official Memcached website, Memcached is a:
“Free and open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.”
In a simple term, Memcached is a distributed temporary object caching system that stores strings and objects in the server’s RAM resulted from processing the database queries, API calls or page rendering. The servers used for this mechanism can be called as Memcached servers.
What is Memcached Used For?
Memcached is used for speeding up dynamic web applications like ecommerce stores, registration/login websites, etc. by reducing the database load. It stores the processed result so whenever a visitor requests the same query again, Memcached can respond to that instead of processing the query and reply. By keeping the server less busy, your visitors will experience a faster loading time and better user experience.
There is an interesting and funny real-world story at the GitHub, give it a read to understand the typical use case of Memcached.
Is your current WordPress hosting underperforming?
Migrate to Cloudways and maximize the performance of your WordPress.
How Does Memcached Work?
Every time a visitor/browser requests the server that requires the database to process and respond, it adds load to the server. Memcached reduces that load by storing data objects in temporary dynamic memory. It saves the data on key-values and checks its memory before sending the browser’s request to the database. If a cache is present, it replies to the request without involving the database to process.
As discussed above, Memcached is a distributed memory object caching system that consists of four major components:
- Client Software: Receives a list of distributed available Memcached servers.
- Client-Based Hashing Algorithm: Chooses the server based on key-values.
- Server Software: Stores data (values) and keys into an internal hash table.
- Server Algorithms: Identifies when to delete the old data and reuse memory.
The above components allow Memcached to store and retrieve data. Each item consists of a:
- Expiration time
- Raw data
When an item is requested, Memcached validates its expiration time to see if the item is still valid before replying to the request. If a cache is not present, it sends the request to the database to process and store the result as an item.
If a server becomes out of the memory, it looks and replaces the expired items. If it still needs any information, it looks for the items not requested for a particular period. By this, Memcached keeps more recently requested information in dynamic memory.
At a high-level Memcached work as follows:
- Memcached checks the requested data if it is stored in the cache.
- Two possible outputs:
– Data is stored in cache: Return the requested data from Memcached without interrupting the database.
– Data is not stored in cache: Execute the requested query, process and retrieve the data, and save the result in memory.
- Whenever there is an update on an item, or an item is expired, Memcached updates its cache and makes sure the fresh content is delivered to the requested client.
How to Use Memcached?
There are two steps involved in using Memcached. First, it must be installed on your hosting server and then your application should be able to support Memcached.
The installation and configuration process is described below:
Install Memcached on Server
Memcached does not require a lot of CPU resources. It is purely dependent on the RAM. If you have a web server of 8GB RAM, but the OS and your websites consume only 4GB, then it is a good idea to assign rest of the RAM to Memcached instances to increase its storage capacity. Give a read to all technical requirements here.
Memcached works on most of the Linux-based servers, installing Memcached is just a two Linux commands away. Generally, you should install Memcached from a package provided by your OS (Debian, Ubuntu, etc.). The Operating System will solve dependencies for you and take care of security updates.
For Debian or Ubuntu users:
Command: apt-get install memcached
Command: yum install memcached
For more information, see the documentation of your respective OS. It is worth mentioning that Nginx, one of the most popular reverse-proxy server comes prepackaged with the Memcached module providing the most flexible Nginx Memcached bundled solution.
IdeaBox – Case Study
Read how Cloudways Helped a WordPress Agency Build Better Products.
Your Ebook is on its Way to Your Inbox.
Check If Memcached Is Running
There are multiple ways to test if Memcached is running. One of them is using Telnet, log in to your server SSH terminal and write below command:
telnet localhost 11211
telnet 127.0.0.1 11211
P.S: The above command will run if the local server is your Memcached server.
If the above command runs successfully, you will get the below result:
Connected to localhost. Escape character is '^]'.
Otherwise, you will get a connection error.
To check some basic stats you can use below command: stats
You will get something like below:
STAT pid 313 STAT uptime 2778636 STAT time 1535727399 STAT version 1.4.21 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 47.119322 STAT rusage_system 48.765342 STAT curr_connections 1 STAT total_connections 151198 STAT connection_structures 3 STAT reserved_fds 20 STAT cmd_get 46 STAT cmd_set 4 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 30 STAT get_misses 16 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 22942 STAT bytes_written 3433252 STAT limit_maxbytes 268435456 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT malloc_fails 0 STAT bytes 106 STAT curr_items 1 STAT total_items 4 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 STAT crawler_reclaimed 0 STAT lrutail_reflocked 0
To check the items, use: stats items
STAT items:2:number 1 STAT items:2:age 2486 STAT items:2:evicted 0 STAT items:2:evicted_nonzero 0 STAT items:2:evicted_time 0 STAT items:2:outofmemory 0 STAT items:2:tailrepairs 0 STAT items:2:reclaimed 0 STAT items:2:expired_unfetched 0 STAT items:2:evicted_unfetched 0 STAT items:2:crawler_reclaimed 0 STAT items:2:lrutail_reflocked 0
To check current memory statistics write: stats slabs
STAT 2:chunk_size 120 STAT 2:chunks_per_page 8738 STAT 2:total_pages 1 STAT 2:total_chunks 8738 STAT 2:used_chunks 1 STAT 2:free_chunks 8737 STAT 2:free_chunks_end 0 STAT 2:mem_requested 106 STAT 2:get_hits 30 STAT 2:cmd_set 4 STAT 2:delete_hits 0 STAT 2:incr_hits 0 STAT 2:decr_hits 0 STAT 2:cas_hits 0 STAT 2:cas_badval 0 STAT 2:touch_hits 0 STAT active_slabs 1 STAT total_malloced 1048560
To quit the connection type quit and press enter.
There is a list of some useful commands to run and inspect Memcached servers.
Memcached Tutorial For Other Platforms:
Memcached for Cloudways Users
Cloudways users do not have to worry about all the technicalities related to WordPress Memcached. All servers launched on Cloudways Platform came pre-installed and activated Memcached.
Navigate to your respective Server → Manage Services and you can see Memcached is pre-installed and activated by default.
Not a Cloudways customer but want to take advantage of Memcached? Get a FREE trial without providing your credit card details.
WordPress Memcached Plugin
There are plenty of WordPress cache plugins available on the market. Most of them support Memcached like W3TC, one of the most widely used cache plugins allow you to configure Memcached. If you are one of the W3TC users, navigate to the General Settings tab and select Memcached from the dropdown, where it says:
- Page Cache
- Minify Cache
- Database cache
Save all settings and do not forget to purge the cache once.
Memcached is considered as one of the ways to speed up a WordPress site iff configured properly as it reduces the database load and keeps the server less busy resulting in better performance and page load time.
Customer Review at
“Beautifully optimized hosting for WordPress and Magento”
Arda Burak [Agency Owner]
Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at [email protected]