Table of Contents
Tired of your Drupal site lagging during traffic spikes? Slow performance not only frustrates users but also hurts conversions. That’s where Memcache comes in. It’s a simple but powerful way to make your site faster by caching data in memory.
In this blog, we’ll explain what Drupal Memcache is, how to connect it with your site, and the performance boost you can expect. Whether you manage your own site or work with a Drupal web development agency for optimization, understanding Memcache can help you take your site speed to the next level.
What is Memcache?
Memcache is a simple tool that makes your website load faster. It stores frequently used data, like page content or user sessions, directly in your server’s memory (RAM), so Drupal doesn’t have to pull it from the database every time. Think of it as your website’s short-term memory. Instead of repeating the same task over and over, Memcache remembers it and responds instantly.
For Drupal sites, Memcache is especially useful. It helps reduce database stress, speeds up page load times, and keeps things running smoothly — even when traffic increases.
How to Set Up and Use Memcache in Drupal?
To take full advantage of Memcache in your Drupal site, you’ll need to set up a few components: the Memcached server itself, a PHP extension that lets Drupal talk to Memcache, and the official Memcache module for Drupal.
This setup ensures that Drupal can store and retrieve cached data from memory, drastically improving performance. Let’s walk through the key steps to get everything working smoothly.
Step 1: Install Memcached on Your Server
Before anything else, you need the Memcached service running on your server. This is the actual background process that stores and serves cached data from memory. Here’s how to install Memcached based on your operating system:
For Ubuntu/Debian:
sudo apt update
sudo apt install memcached libmemcached-tools
For CentOS/RHEL:
sudo yum install memcached
sudo systemctl enable memcached
sudo systemctl start memcached
Once installed, check if Memcached is running properly:
systemctl status memcached
If it shows “active (running),” you’re good to go. This ensures that the caching server is live and ready to handle requests from Drupal.
Step 2: Install the PHP Memcache Extension
Now that Memcached is running, Drupal needs a way to communicate with it. That’s where the PHP Memcached extension comes in — it acts as a bridge between your PHP application and the Memcached server.
For Ubuntu/Debian:
sudo apt install php-memcached
sudo systemctl restart apache2 # Or nginx/php-fpm based on your stack
To verify the extension was installed correctly, run the following:
php -m | grep memcached
If “memcached” shows up in the list, it means your PHP setup can now talk to the Memcached service. Without this extension, Drupal won’t be able to use the caching system.
Step 3: Install and Enable the Drupal Memcache Module
With your server and PHP ready, it’s time to connect Drupal to Memcache. You’ll need to install the official Drupal Memcache module, which provides integration between Drupal’s caching system and Memcached. Use Composer to install the module (recommended for Drupal 10 and above):
composer require drupal/memcache
Then enable it using Drush:
drush en memcache -y
If you want a visual admin UI and additional tools for monitoring:
drush en memcache_admin -y
Once you’ve completed these three steps, your Drupal site is ready to use Memcache. The next step will involve configuring Drupal’s settings.php file so it can start storing cache data in Memcached instead of the database. This setup forms the base of a more scalable, faster Drupal experience, especially for content-heavy or high-traffic websites.
Configure Drupal to Use Memcache
To complete the Memcache setup for your Drupal site, the final step is to configure your Drupal installation to actually use Memcache for storing cached data. Up to this point, you’ve installed Memcached on the server, added the PHP extension, and enabled the Drupal Memcache module. Now, you just need to tell Drupal to route its caching operations through Memcache instead of the default database-based storage.
This is done by modifying your settings.php file, which controls various site-level configurations. You’ll typically find this file in the path:
sites/default/settings.php
Before making any changes, it’s a good idea to back up your existing settings.php file, just in case anything goes wrong. Once you’re ready, add the following configuration code:
$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['container_yamls'][] = DRUPAL_ROOT . '/modules/contrib/memcache/memcache.services.yml';
Explanation:
- servers: This line tells Drupal where to find the Memcached server. In this case, it’s set to 127.0.0.1:11211, which means it’s running locally on the default port.
- bins: Cache bins are used by Drupal to separate different types of cached data. This maps the default cache bin to Memcache.
- cache.default: This line overrides the default cache backend and points it to Memcache, so all standard cache operations are handled in memory.
- container_yamls[]: This loads the Memcache module’s service definitions into Drupal’s dependency injection container, allowing Memcache to integrate properly with the Drupal core.
This configuration is enough to enable Memcache for general caching purposes. Once added, it instructs Drupal to store all cache data in the in-memory server instead of in the database. This helps reduce database queries, speeds up page loads, and improves performance overall, especially noticeable on content-heavy sites or those with lots of logged-in users.
Optional: Store Sessions in Memcache
Beyond the general cache, you can also store user session data in Memcache. This reduces the load on your database even further, particularly on sites with many simultaneous users. Add this line to settings.php:
$settings['session_inc'] = 'sites/all/modules/contrib/memcache/memcache.inc';
Note: Path may vary depending on your Drupal version and installation method. If you’re using Composer and modern directory structures, the file might be located in a different place, like /web/modules/contrib/memcache/memcache.inc.
By storing sessions in memory, your site can handle login/logout actions and user interactions faster, which improves the overall user experience during peak traffic.
Final Step: Clear Cache
After making these changes, you’ll need to rebuild Drupal’s cache so the new settings take effect. Run the following Drush command:
drush cr
This clears all cached data and reloads configuration settings from the updated settings.php file.
Verifying Memcache is Working
After completing the Memcache setup, it’s crucial to confirm that Drupal is actively using Memcache for caching. This verification ensures the performance improvements you expect are actually taking effect. Without checking, you might miss issues like incorrect configuration or Memcache not running properly.
There are several simple methods to verify if Memcache is integrated and working well within your Drupal site:
Option 1: Use the Memcache Admin Interface (Optional Module)
If you have enabled the optional memcache_admin module, Drupal provides a convenient stats page in the admin dashboard. By visiting /admin/reports/memcache, you gain access to real-time insights into Memcache’s performance.
This page displays important data such as cache bins, hit-and-miss ratios, and the current status of Memcached servers. This detailed overview helps you monitor cache effectiveness and quickly identify any potential issues.
Additionally, if users have the right permissions, Memcache stats can be shown on the front end of your site. This is useful for debugging or performance monitoring during development or staging.
Option 2: Test via Command Line
For a quick low-level check, you can connect directly to the Memcached server using a command-line tool like nc (netcat). Running the command below sends a request to the Memcached service to return its current settings:
echo "stats settings" | nc 127.0.0.1 11211
This command outputs a list of configuration details from Memcached. If you receive a response, it confirms the Memcached server is running, accessible, and ready to serve cache data. No response or errors indicate an issue with the Memcached service itself.
Option 3: Use the Devel Module (Optional)
Another way to confirm Memcache is doing its job is by monitoring Drupal’s database queries. The Devel module can help here by showing how many database queries execute on each page load. Before enabling Memcache, query counts are typically higher because Drupal fetches cached data from the database. After enabling Memcache, a significant drop in queries usually means cache hits are coming from memory instead of the database, indicating Memcache is working correctly.
Using these verification methods together gives you confidence that Memcache is integrated properly, your Drupal site is benefiting from faster caching, and your setup is stable and efficient.
Performance Testing: Before Vs. After Enabling Memcache
After Memcache is configured, it’s smart to measure the impact on your Drupal site’s performance. Testing before and after helps you see the real benefits, such as faster page loads, fewer database queries, and lower server load. This proof can also support future optimization decisions. There are several useful tools for performance testing:
- The Devel module shows page execution times and database query counts.
- Chrome DevTools helps track metrics like Time to First Byte (TTFB) and total load time.
- Tools like Apache Bench or Siege simulate traffic to test how your server handles multiple users at once.
Here’s a sample comparison of key metrics:
Metric | Without Memcache | With Memcache |
---|---|---|
Page Load Time | ~850ms | ~320ms |
Database Queries/Page | ~70 | ~15 |
Server CPU Usage | High | Reduced |
Note: These are sample values and may vary depending on your site’s setup.
Running these tests clearly shows how much Memcache can improve your Drupal site’s speed and efficiency. It’s a valuable step for ensuring your caching strategy delivers real, measurable benefits—especially important for busy or resource-intensive websites.
Let’s Conclude
Memcache can make a noticeable difference in how fast and efficiently your Drupal site runs. By storing frequently accessed data in memory, it reduces the load on your database and speeds up page delivery.
Setting it up might seem technical at first, but once configured properly, it runs quietly in the background and boosts overall performance. It’s especially helpful for sites with high traffic or complex content structures.
If you’re looking to improve your Drupal site’s speed and scalability, you can always hire our Drupal development experts. We’ll help you integrate Memcache the right way and get the most out of your site.