Object caching involves storing database queries and, when enabled on your WordPress site, it can help speed up PHP execution times, reduce the load on your database, and deliver content to your visitors faster.
Instead of loading up every part of a website every time a user accesses it, object caching stores database queries that can retrieve content more quickly when it is needed. There are a lot of different types of object caching, such as browser, page, mobile and user caching.
In this guide, we will look into object caching in depth, and explain how it works with WordPress.
Table Of Contents
What is Object Caching in WordPress?
Object caching is a process that stores database query results in order to quickly bring them back up next time they are needed.
The cached object will be served promptly from the cache rather than sending multiple requests to a database. This is more efficient and reduces massive unnecessary loads on your server. In simple terms, object caching allows objects that are used often to be copied and stored at a closer location for quicker use.
An efficient database allows large high traffic websites to serve high quality media all over the world.
Overloading your server will result in poor website performance and a negative user experience. When you set up object caching on your website, it’ll ease the strain on your server and create database queries only when needed.
Here is a basic step by step breakdown of how object caching works.
- When a browser accesses your site, it makes a request for information from the server.
- Your server receives the request, starts to process and serve up the request, and then deliver the object back to the browser.
- With object caching disabled, this database query will happen every time a user accesses your site. With object caching enabled, your server will create a duplicate of every request made in your cache.
- The next time a similar request is made, the cache is checked first and the request is served from there, instead of querying the database.
- If a copy does not exist, a request is made to the database to be processed and compiled. Once it is processed, it is sent back to the browser and a copy is made in the cache for future use.
Object caching can help prevent your server from querying your database unnecessarily. This is highly beneficial for high traffic websites or busy times of day. You may think that this must only apply to large global websites with heavy traffic daily. While this is more true the bigger a website is and the more it is accessed, this can help small sites optimize a practice and deliver a better user experience.
A low traffic site will likely not overload a server and would be able to process, compile and serve pages a few times a day. However, object caching in this case will allow your website to enjoy a light server load and faster serving speeds. A large website, however, will demand more out of its server, and have to process much larger requests at a much higher frequency and process and compile pages quicker.
What Are the Different Types of Object Caching?
Different Types of Caching
Caching can either be client-side caching or server-side caching. As the names state, the difference is in where the cache is stored. Most users may be familiar with a type of client-side caching, browser caching.
A user’s browser makes a copy of a static web page so that when they visit the page next time someone, the content is pulled from their computer’s cache instead of having to make a query the database and wait for a server response. This most often is experienced for most internet users when they visit a high traffic web site regularly, and notice that it loads very quickly.
Object caching is a kind of server-side caching. That means that the cache is stored on the server, and cached queries are served from there. Object caching keeps database query results that have already been loaded and then it serves them up faster upon the next request without having to query the database. We will get very detailed in our discussion of object caching, but page caching and CDN caching are some additional types of server-side caching.
Page caching stores the results of the entire webpage, including HTML and content, so that on its next view, the content can be brought up without WordPress having to load it up again.
This is great for server speeds, as it means WordPress doesn’t need to process and deliver the page every time someone visits it. However, it can mean that those pages will remain out of date if content is updated. You can set expirations if you update your content regularly, but this method of caching can sometimes be frustrating.
Content delivery networks (CDNs) using multiple servers around the different parts of the world to store website files and content. This speeds up the delivery of the web pages and content to users who are on the other side of the world. This provides a reliable user experience, as it allows for content to be served from multiple servers.
Persistent Object Caching: Redis, Memcached, and APC
Persistent object caching takes this process a step further. Instead of caching every page or every object all the time, persistent object caching allows objects that appear multiple times to be cached once, and served whenever needed. It is recommended if you have a lot of repeating content or if you are trying to scale your website. It can eliminate a lot of unnecessary traffic to and from your server, and provide an overall better user experience.
We’ll take a look at some popular persistent object caching tools, including Redis, Memcached, and APC. These are powerful tools that provide fast data storage that will reduce the load on your website’s database. They will also improve the response time and traffic capacity of your website. This takes a lot of strain off of your server by streamlining its basic processes, allowing you to focus on your website content, business, and growth.
Redis is by far one of the leading persistent object caching WordPress plugins. Redis is an open-source, in-memory data structure store. It is a database, cache, and message center all in one. It works with many data structures such as strings, hashes, lists, sets, and more. Redis comes with a number of strong features, including built-in replication, scripting, and on-disk persistence. Redis Sentinel and Redis Cluster provide additional features including high availability and automatic partitioning, respectively.
The in-memory dataset is the key feature of the high performance Redis offers. You can edit an object’s persistence by assigning a time to refresh and dump new data or by logging new changes to update when you restart Redis. Alternatively, this feature can be disbaled in you need content to update regularly.
The most popular WordPress plugin to use is Redis Object Cache, with over 30,000 installs. It is easy to install and setup. Once the plugin is activated, navigate to Settings > Redis and click “Enable Object Cache.”
Memcached is also an open-source distributed memory caching. Similar to Redis, it is used for optimizing traffic for dynamic web processes by lightening the load database. Memcached stores data objects in dynamic memory. An easy way to think about this is as “short-term memory”. All the information is stored on RAM, so once the free memory runs out, the oldest data is pushed out and replaced.
Memcached stores data based on key-values for small arbitrary strings or objects, and is comprised of four core components. Client software provides the client with a list of available servers to use. A client-based hashing algorithm allows a server to be selected based on a key. Server software manages and stores values and keys into a hash table. Lastly, Memcached is, by default, a Least Recently Used (LRU) cache, and will throw out or expire old data as needed.
Memcached assigns each item a key, expiration time, and raw data. When the user requests a piece of content or data, Memcached will first look in the cache to see if it is stored there. If it is, it retrieves the data from there and doesn’t need to check the database. If the data is not in the cache, Memcached will query the database, process the data, and store it in Memcached.
Any time data is changed or expires, Memcached will automatically update the cache to load up fresh content. The software keeps track and recalls all data using hashing algorithms and an internal hash table that is constantly accessed and referenced.
There are a number of compatible plugins that support Memcached, and when installed will check to make sure you have Memcached installed on your server. W3TC is a great plugin that supports Memcached (as well as Redis). Once it is installed and activated, navigate to General Settings > Page Cache Method and select Memcached.
Alternative PHP Cache (APC) is a free, open opcode PHP cache, providing a solid framework for caching and optimizing PHP code. APC is different than Redis or Memcached as it focuses only on PHP caching. By reducing dynamic PHP executions, script executions run efficiently. When a browser makes a page request, the server will parse the code in your PHP script and then generate the resulting HTML code that will display as a page in the user’s browser.
PHP is great for websites that are regularly updated with new content, so that every new visitor gets served a current page. If your PHP script is pulling information from a database, APC will automatically generate and store new code for the next visitor. Having to do this in real time everytime a new user accessed your website can greatly slow down your server. APC streamlines that process to reduce server loads. By using APC redundant PHP script executions are avoided, skipping parsing and compiling. APC makes a record of the the opcode that is executed each time the script is needed.
How APC Works
APC caching is run by the Zend Engine and takes place over five steps. First, APC will read the PHP code from the database and write it into memory. Then, the PHP is changed into tokens called, or Lexicons. This process is called Lexing. The third step is parsing, which processes those tokens to create meaningful expressions.
Next, during compiling, these expressions are organized into opcodes. Execution is the last step, when opcodes are run to produce a final result that will be served to the user’s browser. APC aims to reduce the amount of time the first four steps need to be run, focusing on organization and execution.
There are two ways to install APC, from a repository or using PECL. We’ll look at both ways and see what option works best. Because APC runs at the server level, it cannot be used on shared hosting servers. You will have to have a VPS or dedicated server to install it. The repository method is recommended if possible, as having data on your own server is always preferable. In addition, if you choose this method, nothing additional needs to be performed on WordPress, as the caching is happening at the server level.
Using PECL, the latest source file must be downloaded and compiled for your computer. This can be a good method to use, but is not as reliable as a server level installation as you may be subject to issues regarding updates or compatibility.
Once installation is completed, APC needs to be enabled in the PHP configuration file. IF you installed APC from a repository package, this will already be done for you, and you may just need to restart your database. These settings should be enough for you to get started but you can further configure APC.
One parameter you should change is apc.shm_size, which determines the amount of memory to use for APC. By default, it is set to 30 MB, but it is recommended to raise this number to 64, 128 or even 512 MB depending on your needs, resources, applications and available RAM on the server.
What Are the Benefits of Using Object Caching?
A lot of the tasks required by servers and websites are menial, repetitive, and required for the site to function correctly. Object caching handles a large amount of this work, reducing server loads dramatically. This allows content to be served much quicker while keeping a light load on the server. Object caching is beneficial to both content consumers and providers since it essentially affects the way the concern is delivered and received.
There are a number of additional benefits of efficient object caching. Distributing content caches at different places in the network creates better and faster accessibility for users. When the content is closer and easier to access, this reduces server load, and eventually server costs. Additionally, your server or website becomes much more interconnected and responsive this way. Proximity and access of files makes tasks more efficient and user interactivity quicker.
You will also be able to get better performance out of your hardware if these tasks are optimized. Object caching lets your hardware rest and focus on more demanding work, while content can take advantage of other powerful servers between the content and the consumer.
With object caching, your content also remains available during network peaks or interruptions, as it is being served by somewhere else. In the event of an outage or a heavy load, your website does not have to depend entirely on your server to serve content to your users.
WordPress has its own built-in object cache called WP_Object_Cache. This tool automatically stores any new data from the database to PHP memory to while preventing redundancies. However, WP_Object_Cache caches objects only for a single page load. The object is discarded from the cache once the request is complete, and will have to get rebuilt from the next time a request for that page is made.
This is a great built-in feature for Worpress to integrate, and can help any managed WordPress hosting site function more efficiently by not querying similar requests. However, in order to scale your website, interactivity or reach, this will not be enough or efficient.
Persistent caching solutions can come into play here, as object caching is immensely more powerful when objects can be cached over the course of multiple page requests. This will help optimize your server and database overtime as you continue to add content to your website.
By default, WordPress will have built-in object caching enabled. If you have a low traffic site, or one that does not have much content, then this feature may be enough for you. However, if you are unhappy with your server response speeds, or have a heavy content or high traffic site, consider using one of the persistent object caching solutions above.
FAQ About Object Caching
Object caching can bring about a number of questions about which tool to use, how to install them, and how all this will affect your site. Simply put, object caching has the potential to immediately improve your website’s performance and reduce your server loads, no matter what method you choose. Some methods may be better than others for you, while others may not be an option given your system configuration.
Will It Break My Site?
There is no reason object caching should break your site. As long as you install the tools correctly, your website should work better and faster. If using APC caching and installing via PECL, you may experience site issues due to updates or firmware compatibility. To guarantee this does not occur, use the server installation method for APC, or use another persistent object caching tool.
Can I Use It in Conjunction With Other Caching Methods?
It is recommended to have only once caching tools installed, as this allows the database to be created by one plugin or tool. If your specific purposes may call for you using two or more caching tools, make sure the settings complement each other, and do not perform opposite or redundant actions. If this is not set up correctly, you may experience duplicate files or databases, but not necessarily any site issues. Over time, however, duplicates can take up space and slow down your server response.
What Kind of Performance Results Can I Expect From Using Object Caching?
Once set up, you should see an immediate improvement in server response and server load. Your website should be working faster and at the same time using less resources to serve content. Users from geographical points far away from your server will have better experiences accessing your content. The first noticeable result is an improved user experience.
For larger servers and content producers, your reduced server load may reduce your cost, especially over a longer period of time. Additionally, it frees up a lot of valuable server space.
Object caching is a tool that is used across the web to improve website experiences, reduce server loads, and speed up user interaction. By default, WordPress sites have single session object caching activated, to help optimize experiences for sites by default. For many smaller content providers, this may accomplish their goals. Alternatively, they may not be aware of how they can optimize their content delivery using object caching.
Persistent object caching solutions such as Redis, Memcached, or APC may be required for bigger content producers with more data-heavy websites. This allows for databases to be cached over multiple sessions. For massive websites with videos, pictures, and media, this can mean dramatic changes for user experience and for server speeds, loads, and costs.
Think of how many times you go to Youtube.com, and how much it would have to serve you if it had to build that webpage from scratch every time. Now multiply that by the 2 billion people who use Youtube. Without persistent object caching, this would be a very difficult, time and energy-consuming task, and at that scale, almost impossible.
Check out our Starter Sites built with #ToolWeLove including Toolset, Elementor Pro, and Astra Pro.