Reputation: 18157
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
Upvotes: 5
Views: 1912
Reputation: 18157
The solution was to use redis-store with the namespace param.
Here is my config/production.rb
file.
# App 1
config.cache_store = :redis_store, {path: "/tmp/redis.sock", db:1, namespace: "app1"}
# App 2
config.cache_store = :redis_store, {path: "/tmp/redis.sock", db:2, namespace: "app2"}
Upvotes: 2
Reputation: 18514
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.
Upvotes: 6
Reputation: 39933
A common method for this is one of two things:
app1:key
, app2:key
.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.Upvotes: 2