How to Optimize WordPress with Redis and Docker - Plesk Guide

How to Optimize WordPress with Redis and Docker

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports various data structures and offers extensive customizations. In this article, we’re going to discuss how you can use it to optimize a WordPress website.

WordPress Cache and Redis

WordPress caches internal application objects, like breadcrumbs, menu items, and so on, in the MySQL database. It may be quite taxing since that database also handles queries for page requests. Both combined may increase website load-times.

Redis offers a caching mechanism that substitutes MySQL database. When a user visits a WordPress website, the MySQL queries necessary to generate the page come via Redis, which also caches the results. This helps to reduce loading time.

How Does WordPress and Redis Work Together?

When a user requests a WordPress page for the first time, MySQL queries are performed on the server. Redis caches the result of those queries and stores it. So, when another user requests the same page, Redis provides the information, bypassing the database.

If the query is not cached by Redis, MySQL provides the results and then adds them to Redis cache. When a particular value is updated in the database, the corresponding Redis value becomes invalid. Therefore, you avoid serving bad cache data to the user.

Running Redis with a Docker Container

To simplify the Redis installation, we’re running it in a Docker container. To install one via Plesk, go to Server Management > Docker and search for the image, then click Run (local):

Running Redis with a Docker Container

We’re using the bitnami/redis image, and the following settings (change redispassword to something else, and the IP address to IP of the container):

How to optimize WordPress with Redis - Redis and Docker

Install Redis Object Cache Plugin Via WordPress Toolkit

Now that our Redis server is running properly, we can install WordPress plugin Redis Object Cache to our WordPress instance using Plesk WordPress Toolkit:

Redis Object Cache Plugin Via WordPress Toolkit

Just search the plugin name and install:

How to optimize WordPress with Redis - Redis Object Cache Plugin

Before enabling object-cache in WordPress, you have to add the following lines in your wp-config.php file. You can do that in Plesk via File Manager.

define( 'WP_CACHE_KEY_SALT', 'example.com:' );

define( 'WP_REDIS_PASSWORD', 'redispassword' );

The first one adds a prefix to all cache keys stored in Redis. Thus, allowing you to use object-cache for multiple WordPress instances on the same server. The second protects Redis with a previously specified password.

Redis Object Cache Enabled

Then, you can enable object-cache in the plugin settings page – in WordPress. The final result will look something like the above screenshot.
Let us know if this guide was helpful to you and any feedback you may have on the topic!

How useful was this post?

Click on a heart to rate it!

Average rating 2.7 / 5. Vote count: 7

Oh no, sorry about that!

Let us know how we can do better below

About

Avatar
Paul (Pavel) is a former Plesk engineer who loves writing helpful guides for the community.
    Showing 14 comments
    • Avatar
      Chris
      Reply

      HI

      I have tried to follow this guide.

      Firstly when I create the container in Plesk (obsidian RC3) there is more options shown such s master_password aswell as the redis password.

      If i create it with config to match your example when I try to start the container it says

      Error: {“message”:”cgroups: cannot find cgroup mount destination: unknown”}

      OS: CloudLinux 7

      • Debbie from Plesk
        Debbie from Plesk
        Reply

        Hey Chris, sorry you’ve run into some issues there. Obsidian is not a stable release yet however and we are offering additional support. Have you tried contacting one of our guys? https://support.plesk.com/hc/en-us

      • Avatar
        Paul Mikhaylov
        Reply

        Additionally, as far as I know, Docker in Plesk is not very compatible with CloudLinux in general. You can either use Docker on CloudLinux manually, or host Redis container on a different platform (CentOS, Ubuntu).

    • Avatar
      Reply

      Very nice post “Paul”. You have mentioned every little detail about WordPress properly. It’s areally a complete guide for a WordPress developer. Keep Posting!!

    • Avatar
      Gustavo Salvo
      Reply

      Hello, I have configured everything as you mark, but I can’t make it work.
      The plugin returns “Redis server went away”
      I use centos 7

      • Debbie from Plesk
        Debbie from Plesk
        Reply

        Hey Gustavo,

        Seems it’s best if raise a ticket with our support: https://support.plesk.com/hc/en-us
        Thanks for getting in touch!

      • Avatar
        Ben
        Reply

        I have this same issue. “Redis server went away”. These instructions use to work for all of my sites. Now it does not work.

    • Avatar
      Pascal Brennecke
      Reply

      Hi,
      I’m a Newbie to PLESK and setting up Redis Cache but I want to learn. 🙂
      I’ve got several questions:

      //1. In the Screenshot there is the value “REDIS_MASTER_PORT_N” but it seems to be cut of.
      I set mine just to “REDIS_MASTER_PORT” – is this correct?
      //2. I want to run Redis on my vServer – so I set “REDIS_MASTER_HOST” to the value localhost – is this correct or should I use 127.0.0.2?
      //3. So I installed it on localhost to test it but it gave me an
      # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
      How to solve this?

      I’m running Plesk Obsidian 18.0.20 and Redis 5.0.6 64 bit

    • Avatar
      KOSMETIK SHOP
      Reply

      Hi
      how can i change memory limit? for more performance. Thx

      • Debbie from Plesk
        Debbie from Plesk
        Reply

        Hi, Can you specify which memory limit you’re referring to so that we can better answer your question? Thanks! 🙂

    • Avatar
      Gabriel
      Reply

      Hi,
      regarding this guide I found it very useful as I was trying to do the same but I’ve faced many issues.

      1. If you’re already running a redis-server in the server then the port cannot be the same for internal / external because docker cannot bind to it.

      2. Redis is very lightweight but how much memory is recommended for the docker instance? 1G? 512MB? 2G? With 512MB and 2 users online it uses about 7% of memory in my simple test.

      3. There seems to be a huge bottleneck when using docker instead of the local redis-server. When using local instance the site loads cached pages almost instantaneously but with redis on docker the benefits are really small. Perhaps we’re missing some extra configuration? I’ve read on bitnami issues that docker NAT is causing the bottleneck. Is it just unusuable with docker? Should we just create multiple instances on the local server instead?

      Any hint would help a lot.

    • Avatar
      Alex
      Reply

      Installed on Centos 7. No performance increase detected. Redis requires a lot of tuning. So, it is not a plug-and-play solution, at least for wordpress. I would start with switching from centos to ubuntu. Might be a good performance tuning start

    • Avatar
      Hakan
      Reply

      Hello there,
      There is a note on the Redis screen like this: Your site does not meet the system requirements for the Pro version: The current version (3.0.0) of the PhpRedis extension is too old. PhpRedis 3.1 or newer is required.

      What should I do for this, so how can I update Redis? (Not in terms of WordPress, but in terms of plesk, ie system, host)

      Thank you

    Leave a Comment

    Start typing and press Enter to search