Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Redis server per Rails app?

I've a bunch of rails app on my server that should be able to use Redis as cache engine.

Do I've to start one instance of Redis for each of my application, or does the Redis support scoping?

I'm worried that if I remove one value in one app, the value with the same key will be removed for all of my apps.

I do NOT for example want this to happen.

App 1
Rails.cache.write("key", "value")

App 2
Rails.cache.read("key") => "value"

App 3
Rails.cache.delete("key")

App 1
Rails.cache.read("key") => nil

like image 832
Linus Oleander Avatar asked Jan 19 '23 14:01

Linus Oleander


2 Answers

I suggest running a server for every application. Every additional Redis instance only uses 1 megabyte more memory when empty, so the overhead is small and it is ok to run tens of servers in a single instance. Also an idle Redis server is going to use a minimal amount of memory.

So basically by running multiple servers you are not wasting resources, but instead gaining speed as you'll use all your CPUs or CPU cores, given that Redis is single threaded.

like image 161
antirez Avatar answered Jan 25 '23 11:01

antirez


A common method for this is one of two things:

  • Prefix your keys with some sort of "type" identifier, like app1:key, app2:key.
  • Use separate DBs for each using SELECT. By default, connections start out against DB 0. If you do SELECT 1 for app1, SELECT 2 for app2, etc., you are guaranteed that no other application will clobber that data.
like image 43
Donald Miner Avatar answered Jan 25 '23 11:01

Donald Miner