Algoleigol
Algoleigol

Reputation: 201

Redis for SaaS application

I'm developing a new SaaS application that uses redis for such things as session storage and query cache.

I don't know the best architecture to implement with redis for SaaS applications. It's better to use a single redis database for all tenants and identify each tenant by namespaces or it's better to use a single redis database for each tenant?

I think so it's better to use a single database for all tenants, but i'm not sure about this.

Also if the best option for use redis in a single database, i want to know how to use redis for query cache. If i want to save in cache all products for one tenant how to manage this. I think this would be something similar like this.

HMSET products:tenant_id id "123" name "product name" price "9.99"

If this is the best aproach i don't know how to get one product by id

Thanks in advance

Upvotes: 1

Views: 748

Answers (1)

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

Just Include the product id in the hash key:

HMSET products:tenant_id:123 name "product name" price "9.99"

If you want too use a database per tenant, you have to ensure the max number of database defined in redis.conf is greater or equal to the number of tenants. The default value is 16.

Using a database per tenant could have some minor pros, but nothing decisive:

  • the keys will be shorter, because they wont include the tenant ID ; so they will use less memory. This would be significant only if you have a very high number of keys and a limited memory;
  • you can flush all the keys for a given tenant using FLUSHDB. But does it make sense in your use case?

From my point of view, both solutions are valid.

Upvotes: 1

Related Questions