Namespacing keys in Kredis

ayushn21

Ayush Newatia

Posted on October 6, 2021

Namespacing keys in Kredis

Kredis is a library that provides an abstraction of higher level data structures for Redis. It's really useful and will be included in Rails 7.

I like to namespace all my keys in Redis because it prevents clashes between keys from multiple apps in development locally. It also opens the door to sharing a single Redis instance between multiple apps in production. That's usually not a good idea but for a few side projects, it might be a viable and economical choice.

At first glance it seems like it's really easy to add a namespace to keys in Kredis. There's a Kredis.namespace= method. However it's primarily meant to prevent clashes in parallel testing. If you look at the implementation, it looks like:

def namespace=(namespace)
  Thread.current[:kredis_namespace] = namespace
end
Enter fullscreen mode Exit fullscreen mode

It only sets the namespace on the current thread. That's fine when using a single threaded process like the Rails Console. But in a multi-threaded process like Sidekiq, you're going to be in trouble.

One approach to solve this could be setting the namespace on every thread. However that's pretty messy.

The redis-namespace gem

A clean way to namespacing Kredis is to install the redis-namespace gem. It provides a namespaced proxy to an underlying Redis connection.

To configure Kredis to use this proxy instead of creating its own connection; we need to add the following line in application.rb:

config.kredis.connector = ->(config) { 
  Redis::Namespace.new("my_app:kredis", redis: Redis.new(config)) 
}

Enter fullscreen mode Exit fullscreen mode

Now all keys we set with Kredis will be namespaced with "my_app:kredis". This also makes it easier to filter keys if you're using a GUI tool like Medis to view your Redis data.

πŸ’– πŸ’ͺ πŸ™… 🚩
ayushn21
Ayush Newatia

Posted on October 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Namespacing keys in Kredis
ruby Namespacing keys in Kredis

October 6, 2021

Rails Cache with Redis Hashes
ruby Rails Cache with Redis Hashes

August 22, 2020

RediSearch on Rails
ruby RediSearch on Rails

August 3, 2019