57

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.

ripper234
  • 222,824
  • 274
  • 634
  • 905

3 Answers3

40

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.

A.D.
  • 2,352
  • 2
  • 15
  • 25
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
  • 7
    A namespace isn't a database, the database is indicated by number(0 to 15). Namespace is used to add prefixes – kalelc Jan 29 '18 at 19:03
  • 4
    @kalelc: Since the term "namespace" is not defined in the Redis documentation I don't think that your application of the term here is any more canonical than mine. I was clarifying that each "database" on a given Redis server is an independent "namespace" (that identical keys won't collide). Clearly you can create arbitrary prefixes and treat those as "namespaces" --- but this is not any specially supported feature in Redis; it's just a convention for using it. – Jim Dennis Jan 29 '18 at 20:00
  • Thank Jim for the clarification, Sometimes is confusing when we found namespace in Rails with Redis. – kalelc Jan 29 '18 at 20:11
  • @JimDennis - For some reason same name namespace in separate db are colliding for some reason (using ruby gems). That's weird of course. I will see if I find anything new. – Sachin Nov 24 '18 at 16:30
  • 1
    My bad. I was using custom `$redis` variable and passing it newly create `Redis.new` argument (which is of course not respecting any Rails config) – Sachin Nov 24 '18 at 16:44
8

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
Danny Guo
  • 892
  • 1
  • 14
  • 28
2

If you use Ruby you can look at these gems:
https://github.com/resque/redis-namespace
https://github.com/jodosha/redis-store

Mirek Rusin
  • 18,820
  • 3
  • 43
  • 36
Sergii Mostovyi
  • 1,361
  • 1
  • 15
  • 19
  • I'm not, but I don't understand how they can work technically. I thought the server API does not support them. – ripper234 Jan 03 '12 at 05:53
  • 7
    These are merely ways to provide a mostly transparent wrapper around the Redis access object/handle. Using these simply prepends any key names you use in your code with the "namespace" prefix during any reference to any sort of object in that "namespace." As you surmised there is no isolation of these on the server side, they exist merely as conventions, perhaps even internal standards. – Jim Dennis Jun 10 '12 at 07:58
  • 3
    It tells you at the top of `redis-namespace` library, it's a poor man's namespacing, it'll just prefix everything with "ns:*". – Mirek Rusin Oct 13 '13 at 22:52