Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces in Redis?

Tags:

redis

nosql

Is it possible to create namespaces in Redis?

From what I found, all the global commands (count, delete all) work on all the objects. Is there a way to create sub-spaces such that these commands will be limited in context?

I don't want to set up different Redis servers for this purpose.

I assume the answer is "No", and wonder why wasn't this implemented, as it seems to be a useful feature without too much overhead.

like image 815
ripper234 Avatar asked Dec 23 '11 10:12

ripper234


People also ask

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

How many databases does Redis have?

Managing Databases. Out of the box, a Redis instance supports 16 logical databases. These databases are effectively siloed off from one another, and when you run a command in one database it doesn't affect any of the data stored in other databases in your Redis instance.

What is Colon in Redis?

Colons are a way to structure the keys. They are not interpreted by redis in any way. You can also use any other delimiter you like or none at all.

What is Redis prefix?

Interface RedisCachePrefix Contract for generating 'prefixes' for Cache keys saved in Redis. Due to the 'flat' nature of the Redis storage, the prefix is used as a 'namespace' for grouping the key/values inside a cache (and to avoid collision with other caches or keys inside Redis).


2 Answers

A Redis server can handle multiple databases... which are numbered. I think it provides 32 of them by default; you can access them using the -n option to the redis-cli shell scripting command and by similar options to the connection arguments or using the "select()" method on its connection objects. (In this case .select() is the method name for the Python Redis module ... I presume it's named similarly for other libraries and interfaces.

There's an option to control how many separate databases you want in the configuration file for the Redis server daemon as well. I don't know what the upper limit would be and there doesn't seem to be a way to dynamically change that (in other words it seems that you'd have to shutdown and restart the server to add additional DBs). Also, there doesn't seem to be an away to associate these DB numbers with any sort of name nor to impose separate ACLS, nor even different passwords, to them. Redis, of course, is schema-less as well.

like image 195
Jim Dennis Avatar answered Oct 16 '22 11:10

Jim Dennis


If you are using Node, ioredis has transparent key prefixing, which works by having the client prepend a given string to each key in a command. It works in the same way that Ruby's redis-namespace does. This client-side approach still puts all your keys into the same database, but at least you add some structure, and you don't have to use multiple databases or servers.

var fooRedis = new Redis({ keyPrefix: 'foo:' });
fooRedis.set('bar', 'baz');  // Actually sends SET foo:bar baz
like image 6
Danny Guo Avatar answered Oct 16 '22 09:10

Danny Guo