40

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.

2 답변


32

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 namespace isn't a database, the database is indicated by number(0 to 15). Namespace is used to add prefixes - kalelc
  • @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
  • Thank Jim for the clarification, Sometimes is confusing when we found namespace in Rails with Redis. - kalelc
  • @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
  • 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

1

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


  • I'm not, but I don't understand how they can work technically. I thought the server API does not support them. - ripper234
  • 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
  • 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

Linked


Related

Latest