# wp: How to fix WordPress crashes due to Redis memory

- Link: https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/
- Published: 2024-01-16T00:33:19-08:00
- Author: rich

OK, so after I got the performance of WordPress up to decent with the help of [redis](https://richtong.net/2024/01/01/wp-be-sad-wordpress-is-100-cpu-and-how-to-fix/)
and other tricks, this site went from a horrible 9 seconds to a very decent 1.5 
seconds. But, I did find that it would become unavailable 4-6 times a day. I thought
this was because I had exceeded 100% CPU and because of some plugin. But, I was 
wrong. The answer is that Redis needs a max memory and policy for kicking things
out of memory that is not the default.

## Redis error on the home page

Well, the first hint that I was wrong was the error that on the home page, could
not connect to the redis database. This is the in-memory database that caches MySQL
queries to improve performance

## Redis default does not work for low-memory systems

The default for Redis set in /etc/redis/redis.conf is there is no maximum memory
which means that if the system runs out of memory, then Ubuntu will end the process
and this causes WordPress to lose its redis connection which then causes redis to
automatically restart. That’s why I was seeing 20 minutes of unavailability, so 
the solution is to change the settings.

## Looking at the logfile

So onto [checking](https://betterstack.com/community/guides/logging/how-to-view-and-configure-linux-logs-on-ubuntu-20-04/)
out /var/log/syslog and I saw that redis-server was [stopping](https://dba.stackexchange.com/questions/72661/redis-takes-up-all-the-memory-and-crashes)
all the time. That was a great hint!

## The first try is to set maxmemory

So trying to see [maxmemory](https://serverfault.com/questions/615744/redis-server-memory-increased-to-22gb-and-crashed-oom)
seemed like a good idea. Note that it takes a long time to [service](https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-20-04)
[redis](https://stackoverflow.com/questions/33115325/how-to-set-redis-max-memory)
so instead of doing that it is [easier to run `redis-cli`](https://devcoops.com/redis-start-stop-service/)
and change settings [dynamically](https://www.dragonflydb.io/faq/how-to-restart-redis):

    ```wp-block-code
    redis-cli
    auth <your password>
    config get maxmemory
    # to see what it is set
    config set maxmemory 1gb
    config set maxmemory-policy volatile-lru
    ```

Then when it looks like it is right and running, you can edit redis.conf and restart
the service. I tried a random [cache policy](https://redis.io/docs/reference/eviction/)
volatile-lru because it sounded good, but it turns out that this also needs the 
TTL set on keys, and if you don’t have that set, then the thing crashes (see below).

## Swapfile settings on low-memory machines

A long time ago, I had set up a [swapfile](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04)
for my el-cheapo 1GB virtual machine and made it 3GB worth to prevent it from crashing,
but it does make it super slow if you access it.

## The first settings didn’t go well, smaller memory and allkeys-lru needed

The first setting I tried was to set maxmemory=1gb. Turns out by the way that although
it says it only understands bytes, it does understand gb and MB which is nice. But
I got a [strange message](https://wordpress.org/support/topic/oom-command-not-allowed-when-used-memory-maxmemory/):

    ```wp-block-code
    OOM command not allowed when used memory > 'maxmemory'
    ```

The problem looks like a misconfiguration so I tried `maxmemory-policy=allkeys-lru`
and this seemed to work.

The second thing is that with only a 1GB machine, trying a 1GB cache seems very 
large and going back to a swap based cache kind of defeats the purpose, so I moved
it to a 256mb maxmemory and it seems to work.

## The last step JetPack no longer connects

OK, the final step to getting everything running is that JetPack and WordPress were
no longer connected even with everything running. The [recommendation of disconnecting](https://jetpack.com/support/reconnecting-reinstalling-jetpack/)
and then reconnecting JetPack from the website admin console worked great.

### Share this:

 * [  Mastodon ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=mastodon)
 * [  LinkedIn ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=linkedin)
 * [  Facebook ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=facebook)
 * [  WhatsApp ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=jetpack-whatsapp)
 * [  Email ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?subject=%5BShared%20Post%5D%20wp%3A%20How%20to%20fix%20WordPress%20crashes%20due%20to%20Redis%20memory&body=https%3A%2F%2Ftongfamily.com%2F2024%2F01%2F16%2Fwp-how-to-fix-wordpress-crashes-due-to-redis-memory%2F&share=email)
 * [More](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/#)

 * [  Telegram ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=telegram)
 * [  Print ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/#print?share=print)
 * [  Pinterest ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=pinterest)
 * [  Tumblr ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=tumblr)
 * [  X ](https://tongfamily.com/2024/01/16/wp-how-to-fix-wordpress-crashes-due-to-redis-memory/?share=twitter)

### Like this:

Like Loading…
