Karan Desai
Karan Desai

Reputation: 3142

Why using multiple database in same instance a bad idea in Redis?

I am new to redis therefore I don't know more about its complex technicalities. But let me put my scenario here: I am running two websites from same server and I wanted redis to work on both. On searching, I found that I can do this by assigning different index to different db on same server instance like below:

//In my first website (development)
Idatabase dbOfDev=_conn.GetDatabase(0);

//In my second website (production)
Idatabase dbOfProd=_conn.GetDatabase(1);

This was ideal for me since I could cache both my database in same instance. But then I bumped into What's the Point of Multiple Redis Databases? and How do I change between redis database? links which says "Use of multiple database in same server instance is discouraged and deprecated". Though these links do try to explain the reason behind it, being a beginner, I am still not able to understand its deep technical aspects.

Can anyone explain the reason in simpler terms as why using multiple redis db of same server instance is discouraged. Also, in simpler terms, how can I manage caching of both my websites on same server without the above said approach?

Upvotes: 12

Views: 12199

Answers (2)

for_stack
for_stack

Reputation: 22886

how can I manage caching of both my websites on same server without the above said approach?

You can use different key tag for each website. Say, name the two websites as A and B. For all keys of website A, give each key a prefix(key tag): A:. On the other hand, give each key for website B another prefix: B:. In this way, you can have a unique key namespace for each website.

SET A:key1 val1
SET A:key2 val2
LPUSH B:key1 1
SADD B:key2 val

Also check this answer for more solutions.

Can anyone explain the reason in simpler terms as why using multiple redis db of same server instance is discouraged.

AFAIK, multiple databases feature is NOT discouraged and deprecated. It's a method to isolated key namespaces for different applications. However, the author of Redis consider Redis multiple database errors my worst decision in Redis design at all, since it makes Redis internals more complex.

Redis is single-threaded, so compared to multiple databases, multiple Redis instances can take advantage of multiple cores. If you have multiple databases in one Redis instance, you can still only use one core. Also Redis instance itself has little memory footprint, so you don't need to worry about multiple Redis instance costs you too much.

Redis is very fast, and normally the bottleneck is network bandwidth, NOT CPU. So normally you CANNOT get too much gain by using multiple Redis instances. However, if one of your application needs to do some slow commands on Redis, and don't want it to block other applications, you can have a separate Redis instance for the slow application, and have another Redis instance for other fast applications.

Also note that Redis Cluster doesn't support multiple databases.

Personally, I like this multiple database feature. Normally, if I run a Redis instance, not Redis Cluster, I'll put my data into some database other than the default database, i.e. database 0, to avoid incidentally login Redis and do some horrible things on the default database. Also it's very easy to implement a double buffer with multiple databases, e.g. write data to a new database, when it's done, use the SWAPDB command to swap the old DB and new DB automatically and efficiently.

Upvotes: 15

Adam Eri
Adam Eri

Reputation: 965

It is not. If you are building a multi-tenant application, supporting multiple websites, it does make sense. And if one of the websites needs to scale more rapidly, you can setup a different instance (or cluster) for that one alone and migration is much simpler.

Upvotes: 3

Related Questions