Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to namespace keys on redis to avoid name collisions?

I want to use redis to store some of my own key-value pairs, however some of my modules already use it. The redis express session store for session data, as well as the redis adapter for socket io. So my question is simple, how can I create or designate a database/namespace to store my own keys without key collisions? I am using the node-redis driver.

like image 436
naughty boy Avatar asked Aug 28 '16 14:08

naughty boy


People also ask

Does Redis have namespace?

Redis does not support namespacing. Usually, key name conventions are used to mimic namespaces. A common way of adding namespaces to Redis keys is by prepending a namespace (that is, namespace:key_name). Some Redis clients support addition of a prefix to all Redis keys.

What is Keyspace in Redis?

Redis keyspace notifications allow you to subscribe to PubSub channels. Through the channel, clients receive published events when a Redis command or data alteration occurs. These notifications are useful when an application must respond to changes that occur to the value stored in a particular key or keys.

How many keys can Redis handle?

Redis can handle up to 2^32 keys, and was tested in practice to handle at least 250 million keys per instance. Every hash, list, set, and sorted set, can hold 2^32 elements. In other words your limit is likely the available memory in your system.

What are multi key operations Redis?

Redis Cluster supports multiple key operations as long as all of the keys involved in a single command execution (or whole transaction, or Lua script execution) belong to the same hash slot. The user can force multiple keys to be part of the same hash slot by using a feature called hash tags.


Video Answer


1 Answers

Solution 1: Store data of different modules in different Redis instances

The most strictly isolation is storing data of each module in an individual Redis instance, i.e. an individual Redis process.

Solution 2: Store data of different modules in different databases of a single Redis instance

A Redis instance can have multiple databases, and you can configure the number of databases in the config file. By default, there are 16 databases.

These databases are named with a zero-based numeric index. With the select command, you can use the ith database. After the selection, any subsequent commands will operate on the ith database.

So, if you assign an independent database for each module, you can avoid name collisions.

NOTE: this solution DOES NOT work with Redis Cluster. Redis Cluster only allows you to use the 0th database.

Solution 3: Create namespace with key prefix

If all of your data has to be stored in a single database, you can still implicitly create a namespace with key prefix. For each module, all data of this module should have the same key pattern: ModuleName:KeyName, i.e. each key of this module has the same prefix: ModuleName.

Since each module has a different name, there won't be any name collisions among these modules:

// Set keys for module1
SET module1:key1 value
SET module1:key2 value

// Set keys for module2
SET module2:key1 value
SET module2:key2 value

NOTE: this solution also works with Redis Cluster.

like image 104
for_stack Avatar answered Sep 28 '22 00:09

for_stack