Scott's Recipes Logo

Solving Redis ERR DB index is out of range

Pizza courtesy of Pizza for Ukraine!

Donate Now to Pizza for Ukraine

 

Last Updated On: 2025-09-01 04:31:52 -0400

I was recently working on a Rails app and got this bit of craziness:

Redis::CommandError: ERR DB index is out of range

I'm running a rake task to seed my db.  The error is coming from:

/home/deploy/anziani_app/releases/20220912170302/app/models/relationship_type.rb:13:in 
`block in <class:RelationshipType>'

which is:

  after_create_commit  -> { broadcast_prepend_later_to :relationship_types, 
    partial: "relationship_types/index", locals: { relationship_type: self } }

Thoughts?

Chris from GoRails hopped into the fray with this bit of razor like insight:

What's the ID of your redis db?
It only has like 16 by default

And that defined everything in one fell swoop. Thank you Chris!!!

Confirming The Problem

The first step is to confirm the problem. I started by sshing into my web server and then using this command:

redis-cli config get databases

and that failed as the Redis server wasn’t local.

Note: If you don’t have the redis cli tools, then you need to run:

sudo apt install redis-tools

Sigh. Ok this means I need to know what my redis IP address is and I had no idea. Once upon a time though, I documented a hello world redis thing in Rails console and I be that would tell me the IP address so I started Rails console and did:

require 'redis'
redis = Redis.new
=> #<Redis client v4.8.0 for redis://10.138.188.253:6379/33>

ding. That’s just what I need. This makes my above command:

redis-cli -h 10.138.188.253 -p 6379 get databases
(nil)

And that tells me I’ve exceeded my 16 database limit. And it should be noted that this is a redis configuration issue — not a “need to create more databases”. My intuition is that you are essentially telling Redis to reserve more internal memory for databases but that’s just a guess.

Fixing the Problem

The solution to this is as follows:

  1. Connect to your Redis host via ssh.
  2. Edit your redis config file which should be done with sudo nano /etc/redis/redis.conf
  3. Search the file for databases and change it from the default of 16 to:

    # Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT where # dbid is a number between 0 and 'databases'-1 databases 32

I doubled it from 16 to 32.

The final step is restart redis:

/etc/init.d/redis-server restart

After that verify that Redis came back ok with:

ps auwwx | grep redis
redis    23465  0.0  0.1  70452  4640 ?        Ssl  17:39   0:00 /usr/bin/redis-server 127.0.0.1:6379

References

  1. Stack Overflow on how to Fix it.
  2. Stack Overflow on Connecting to Remote Redis Server
  3. Me on Hello World in Redis
  4. Restarting Redis
  5. Redis Configuration