Partitioning Redis database

Refresh

March 2019

Views

390 time

1

I am new to Redis. I am using redis-py for doing my operations on the database. I wish to partition the database so that I can perform experiments in one and store my important information in another. Is it possible???

I have been looking for the meaning of db in:

r= redis.StrictRedis(host = 'localhost', port =6379, db =0 )

but I am not able to find any clean documentation yet. I think db indicates the database partition but have not able to find anything similar.

Any help with this will be highly appreciated. Thanks!!

1 answers

3

Yes. One Redis instance can contain multiple databases and functions exactly as you require.

The Redis configuration file will tell you how many databases your instance has. The default is something like 16 from memory.

On Ubuntu, this file is in /etc/redis/redis.conf.

There will be a section that looks like this:

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

You can change this and restart Redis (again on Ubuntu) using:

sudo /etc/init.d/redis-server restart

With redis-py, you can choose which you connect to with the db parameter, as you've already gleaned.

From the command line you can connect to a specific DB with the -n flag:

redis-cli -n 3

Inside the redis client, you can switch with the SELECT command:

select 3

We use multiple Redis databases (over 100 in fact), but this would be discouraged for what you're doing. Most folks would encourage you just to choose a key naming convention that provides name-spacing in a single db.

Something like data:<a>:<b> and result:<x>:<y> might be example keys for your use case.