DanP
DanP

Reputation: 6478

Flushing portions of a Redis cache

I'm investigating the use of Redis in an asp.net mvc application using the ServiceStack.Redis client and a single Redis instance running on a remote machine.

Our cache is broken up into 3 major "areas" (Asp.net output cache, NHibernate second level cache, application cache) and I would like to have the ability able to "flush" all of the keys in these areas individually.

In Couchbase (which we currently utilize) this would be accomplished by using separate buckets with a client instance pointing to each one. We could then flush all values in these buckets using a single call from the appropriate client instance.

Is there a way to accomplish a setup like this using Redis? If so, how do I approach this from the client/server side?

Upvotes: 1

Views: 2572

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49932

I can think of three approaches to achieve that.

Shared Databases - Worst Approach (as in highly recommended not to pursuit)

Redis supports shared databases that are basically separate keyspaces managed by the same server. After connecting to your Redis, you switch between the databases with the SELECT statement and you can use the FLUSHDB command to flush each one individually.

Pro:

  • This is the simplest way to get exactly what you need.

Cons:

  • Shared databases share the same Redis process. Since Redis is (mostly) single-threaded, the practice of using shared databases is not recommended as operations in one database can interfere with other databases running on the same server.
  • It isn't clear whether support for shared databases will be continued in future versions of Redis.

(For more information on Shared vs. Dedicated Redis databases, check out my post at http://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances)

One Database, Different Key Prefixes - Slightly Better but still...

You could use a single Redis database and prefix you keys according to the "area" they belong to (e.g. keys that go into the ASP.Net area will be prefixed with 'asp:' etc...). To delete an area, iterate through the keyspace with the SCAN command using the relevant key name pattern and DEL the results it returns.

Pros: can't think of any Cons:

  • Same as the above + more coding to implement the SCAN/DEL routine.

Dedicated Databases - Recommended Approach

Use a separate Redis instance for each area, plain and simple. Set up your remote machine to run 3 Redis servers, each managing its own keyspace. To flush, connect to the relevant database and do FLUSHDB or FLUSHALL.

Pros:

  • Flush an area with a single command
  • Leverage multiple cores on the remote machine

Con:

  • Could mean a little more administrative effort to set up 3 Redis servers.

Lastly, if you're looking for a way to use Redis without the hassle, I urge you to consider Redis Cloud as an option for a hosted Redis service in the Cloud. We are the only service provider that lets you set up multiple, dedicated Redis databases in the same subscription at no extra cost.

Upvotes: 2

Related Questions