Article directory
Is a single server running multiple WordPress websites sharing Memcached causing data overwriting or cross-connection issues?
This article provides a step-by-step guide on how to perfectly isolate transient and object cache data across different sites by configuring a separate cache prefix (WP_CACHE_KEY_SALT) and object cache scripts.
100% eliminates data conflicts, allowing multiple sites to enjoy the ultimate memory-level acceleration experience simultaneously, and includes safety tips to prevent pitfalls in production environments!
A few days ago, a friend asked me about his three WordPress websites running on his server. He wanted to configure Memcached to speed them up, but he was worried that the data would get messed up.
I laughed at that moment because I had fallen into this trap myself.

Honestly, Memcached is incredibly easy to set up for a single site, but it gets interesting when it comes to multiple sites coexisting. Why?
By default, Memcached puts all websites' transient and object caches into the same pool. Imagine if websites A and B happen to use the same cache key—that would cause chaos. A's data would directly overwrite B's, and the two websites would start "interfering" with each other's data.
What I encountered was even more ridiculous: a product details page on an e-commerce website was displaying the content of an article from another blog website. I was completely stunned, thinking, "What is this?!"
It took me a whole night to figure it out; it turned out to be a cache key conflict.
So today I'm sharing my configuration method in the hope that it can help friends with the same needs. I'm still exploring it myself, and some of my ideas may not be fully developed, but this method has worked fine for me.
Server environment preparation
Before you begin configuring WordPress, you must ensure that the server has Memcached service and the PHP Memcached extension installed.
How do I install Memcached using HestiaCP?
Taking Ubuntu as an example, it involves just three lines of commands.
sudo apt update
sudo apt install memcached php-memcached
sudo systemctl restart php8.x-fpm # 根据你的 PHP 版本改After installation, remember to check if the Memcached service is running.
sudo systemctl status memcachedIf you see "active (running)," it means there's no problem.
Note that package names may differ across systems. For Debian-based systems, this command is generally used; however, for CentOS or other distributions, you might need to use yum or dnf to install.
I was using Ubuntu 20.04 at the time, and the whole process went fairly smoothly.
Core operation: Configure cache prefix
This step is the soul of the entire configuration and the key to preventing data confusion.
You need to add a unique cache prefix to the wp-config.php file in the root directory of each WordPress website.
Open the wp-config.php file of the first website and find this line of comments.
/* That's all, stop editing! Happy blogging. */Add above this line
// 网站 A 的配置
define( 'WP_CACHE_KEY_SALT', 'site1_unique_string_' );Then open the wp-config.php file on the second website and add a similar configuration.
// 网站 B 的配置
define( 'WP_CACHE_KEY_SALT', 'site2_unique_string_' );I'm telling you, the string following WP_CACHE_KEY_SALT must be absolutely unique . I suggest you use your website domain name or abbreviation, such as site1_com_ or site2_net_.
I was lazy and used numbers, but three months later I completely forgot which number corresponded to which website, and I had to look them up one by one, which was a real headache.
Think about it: Memcached works by storing and retrieving data based on cache keys. If you set a unique prefix for each website, their data will naturally be isolated and won't interfere with each other.
This is like giving each website its own locker. Although they are all in the same room, the keys are different, so the data won't get mixed up.
Enable object caching
After configuring the prefix, the next step is to enable WordPress to actually use Memcached.
There are two options here, and I've tried both. Each has its own advantages.
Option 1: Plugin method (most hassle-free)
Log in to the WordPress admin panel and install a caching plugin that supports Memcached.
I recommend using W3 Total Cache ; it's quite stable. After installation, in the plugin settings, select Memcached as the cache type, enter 127.0.0.1 for the server, and the default port 11211, then save.
If you want a purer solution, you can directly install the Memcached Object Cache plugin, which is dedicated to this task and has no other miscellaneous functions.
To be honest, I initially thought the plugin approach was pretty good, since it's convenient. But later I found that some plugins come with a bunch of features I don't need, and I'm always worried about updates, afraid that they'll suddenly add some feature that messes up my configuration.
Option 2: Manually deploy object-cache.php (more controllable)
If you're a bit of a perfectionist like me and don't want to install too many plugins, you can use this solution.
Download the Memcached Object Cache plugin from the official WordPress plugin repository, or find the latest object-cache.php file on GitHub. Then, upload the extracted object-cache.php file to the wp-content directory of each website via FTP or SSH.
Note that it is the root directory of wp-content, not the plugins subdirectory.
After you add it, WordPress will automatically recognize and activate the cache. You don't need to configure anything, because it will read the WP_CACHE_KEY_SALT you set earlier in wp-config.php.
I did want to save time, so I used this solution mainly because it's controllable. If a problem arises, I know where to look for solutions, and I won't be caught off guard by automatic plugin updates.
Verify that the configuration is effective.
After you've finished preparing the sample, you'll definitely want to know if it was successful, right?
The simplest way is to check the status of Memcached. Connect using telnet or nc tools.
echo "stats" | nc 127.0.0.1 11211Then you'll see a bunch of statistics. Pay special attention to the values of `cmd_set` and `get_hits`, and refresh the page a few times to see if they're increasing. If they are, it means the cache is working correctly.
Another more intuitive way to verify this is to open both websites simultaneously and see if they interfere with each other. If website A does not display content from website B, then it basically means the prefix is working.
I did an even more rigorous test at the time. I set different transient values on two different websites and then used the nc tool to directly query the data in Memcached. I could indeed see two key-value pairs with different prefixes, with no confusion at all.
At that moment, I really felt like I had accomplished something great, and just thinking about it makes me feel awesome.
Don't ignore safety issues
Since all websites share a single Memcached instance, there's a security risk you should be aware of.
In theory, if other users are on the server, they could read your cached data through a local port. While this requires a certain level of technical expertise, it's always wise to be cautious.
Therefore, you must ensure that your Memcached service absolutely does not listen to public IP addresses . Make sure this line is present in the `/etc/memcached.conf` configuration file.
-l 127.0.0.1This way, Memcached will only listen to the local loopback address and will not be accessible from outside.
If you have extremely high security requirements, or if your server hosts websites that other users are uneasy about, I suggest you consider using Redis instead of Memcached. Redis supports multi-database isolation and password verification, making it much more secure.
However, Redis is slightly more complex to configure than Memcached. If you are just using it for personal or small team purposes, Memcached with the 127.0.0.1 listening limit is basically sufficient.
Some pitfalls encountered
Finally, let me share a few pitfalls I encountered, hoping to help you avoid some detours.
Make sure your PHP extension version is compatible . I previously encountered a situation where I installed the php-memcached extension, but WordPress didn't recognize it. I later discovered it was due to a PHP version mismatch. Make sure you check your PHP version carefully when installing extensions to avoid installing the wrong one.
Make sure you get the path to the object-cache.php file right . It must be placed in the root directory of wp-content, not the plugins directory. A friend of mine put it in the wrong place and spent ages trying to fix it without success.
Don't use short strings for your cache prefix . I initially used strings like a_ and b_, but I found that they were too short and easily clashed with WordPress's own generated cache keys. Use at least a seven- or eight-character random string or a domain abbreviation.
When clearing the cache, remember to clear the cache for both websites . Once, I only cleared the cache for one website, but the other website still retained the old data, causing inconsistencies between the two websites. It took me a long time to figure out the problem.
To be honest, I don't know if these experiences will be useful to everyone, but I have shared them without reservation.
Write at the end
Configuring Memcached for multiple sites isn't technically complicated; the core issue is configuring the cache prefix. However, the devil is in the details. Issues with version matching, file paths, and security configurations can cause you considerable trouble.
I've been using this method for almost a year now, running three WordPress sites, and so far I haven't encountered any major problems. If you're doing something similar, you might want to give it a try.
By the way, if you have any better solutions or have encountered any interesting pitfalls, feel free to leave a comment and share them. I'd love to learn from you.
After all, the more people know about this, the fewer people will fall into the trap, right?
Since you've read this far, if you found it helpful, please like and share it. If you want to receive updates first, you can also follow me!
Thank you for reading my article. See you next time.
Hopefully, the article "How to Share Memcached with Multiple WordPress Websites on the Same Server? The Ultimate Configuration Guide to Prevent 100% Errors" shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ) will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-34368.html
