Switching from Redis to KeyDB

I just read a few pages with benchmarks about Redis vs KeyDB and I wanted to give KeyDB a long time a try – it was on my ToDo. Here are (finally) three examples on how to switch from Redis to KeyDB.

Install

The following will add the repository and key, update the package list and install keydb. I used this in Debian Bookworm.

echo "deb https://download.keydb.dev/open-source-dist $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/keydb.list
wget -O /etc/apt/trusted.gpg.d/keydb.gpg https://download.keydb.dev/open-source-dist/keyring.gpg
apt-get update
apt-get install keydb

Switching: 3 Examples

Please take note, that what I show here is not recommended for the software I’m using. E.g. I show you how to use KeyDB instead of Redis with Nextcloud: This is cool and funny no question. And maybe it improves the speed of your installation a lot – But still this is not the recommended way. Same for paperless-ngx. So if you do this, you should know what you’re doing, why you’re doing it and you should expect problems.

Replacing my Multi-Instance-Redis with KeyDB

I wrote about setting up a multi-instance-redis systemd script in my blog and when I played around with keydb I noticed that both keydb as well as redis already come with such a script – it’s just called redis-server@ and keydb-server@ instead of redis@ and keydb@. This makes it a little bit easier. Using Redis with Amavis is one of it’s cool features I wrote about.

So using KeyDB as a drop-in replacement for redis was in my case as simple as:

cp redis-amavis.conf ../keydb/keydb-amavis.conf
cp redis-spamassassin.conf ../keydb/keydb-spamassassin.conf
sed -i 's_redis_keydb_g' keydb-*
sed -i -e 's@^logfile .*@logfile /var/log/keydb/keydb-server-myname.log@' /etc/keydb/keydb-amavis.conf
sed -i -e 's@^pidfile .*@pidfile /var/run/keydb-myname/keydb-server.pid@' /etc/keydb/keydb-amavis.conf
chown keydb:keydb /etc/keydb/*

My configuration looks like this:

# cat /etc/keydb/keydb-amavis.conf 
include /etc/keydb/keydb.conf

port 6377

pidfile /var/run/keydb-amavis/keydb-server.pid
logfile /var/log/keydb/keydb-server-amavis.log
dbfilename amavis.rdb
dir /var/lib/keydb

maxmemory 300M

Of course you can also copy the keydb configuration instead of including it like I do above.

systemctl disable redis@amavis
systemctl stop redis@amavis
systemctl enable keydb-server@amavis
systemctl start keydb-server@amavis

This was all. Test if all works and remove redis.

Replacing my Nextcloud Redis with KeyDB

My Redis in Nextcloud uses a Socket for communication instead of TCP:

redis 749 0.2 0.4 72436 17644 ? Ssl Apr07 35:18 /usr/bin/redis-server unixsocket:/var/run/redis/redis-server.sock

In the nextcloud configuration file the relevant parts are:

  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '/run/redis/redis-server.sock',
    'port' => 0,
    'timeout' => 0.0,
  ),

So replacing Redis with KeyDB here is as simple as changing the host part above to:

'host' => '/var/run/keydb/keydb-server.sock',

If you use Redis for Session Storage you may also need to change that in your PHP.ini (search for session.save_path). In my case:

conf.d/20-redis.ini:session.save_path = "unix:///var/run/redis/redis-server.sock"

to

conf.d/20-redis.ini:session.save_path = "unix:///var/run/keydb/keydb-server.sock"

Also in /etc/keydb/keydb.conf set:

Port 0
unixsocket /var/run/keydb/keydb-server.sock
unixsocketperm 770

You may need add www-data user to keydb group in /etc/group. Restart php and check if you can login to nextcloud. That should be it. Always check with e.g. keydb-cli if it works:

keydb-cli -s /var/run/keydb/keydb-server.sock

And use e.g. INFO to get some informational data.

Replacing my paperless-ngx Redis with KeyDB in docker compose

At first I thought this might be more tricky because I am using docker compose here. But in fact it was even more simple than the previous replacements. I just needed to edit the docker-compose.yml:

(1) I renamed all redisdata to keydbdata.

(2) I replaced the image: line with image: eqalpha/keydb:latest

(3) I ran docker compose down, docker compose pull and docker compose up -d

(4) I’ve checked in the paperless GUI (settings -> System Status) if Redis Status is OK.

The relevant parts of my docker-compose.yml:

services:
  broker:
    image: eqalpha/keydb:latest
    restart: unless-stopped
    volumes:
      - keydbdata:/data

[..snip..]

volumes:
  data:
  pgdata:
  keydbdata:

That’s all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.