Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couchbase as a cache and cache invalidation

I'm thinking about using Couchbase as a cache layer. I'm aware of the many advantages provided by Couchbase, like the easy scalability. But what interests me more is the rich document model of couchbase, compared to the simple key-value one of memcached.

My RDBMS is SQL Server, and we use NHibernate. The queries and the database are already quite optimized and I think that caching is the best option for further scaling.

My project is to implement a simple relationnel model between entities (much simpler than the one in the RDBMS), to handle invalidation. When an entity is invalidated (removed from cache) by the application, all dependent entities could also be removed. The logic of defining the dependencies between entities would be handled at the application level by a dedicated component. There would be 10 or 12 different entities (I don't want to cache all my application domain).

My document model in Couchbase would look like this:

  • Key (the one generated by the application), keys' format depends on entity type
  • Hashed key (to have a uniform unique key accross all entities)
  • Entity
  • Dependencies - list of hashed keys of the entities that must be removed when main entity is removed

So my questions are:

  • On invalidation, we would need to resolve a graph of dependencies (asynchronously). Is it fast to look for specific keys with around 500k entities?
  • Any feedback on the general idea?

Maintaining the dependencies between entities can be quite simplified, and might not be such a big issue.

Pierre

like image 482
Pierre Murasso Avatar asked Jan 22 '14 17:01

Pierre Murasso


Video Answer


1 Answers

I use Couchbase 2.2 in production as a persistent cache layer and really happy with it (running about 2M documents). My app getting really fast gets (1 millisecond). Your idea is valid and I don't see anything wrong with using Couchbase as a entity storage for invalidation. Its a mature and very stable product.

You are correct in your entity design. You can have a main json doc that has list of references to other child documents. So that before deleting main document you will delete all children first.

Also, not sure if its applicable in your case, you can take advantage of Couchbase ability to expire documents. When you insert key/value(json doc) you can specify TTL(time to live) if you know it upfront. This way you don't need to explicitly delete entities from Couchbase.

Delete operation itself is fast (you can run it as asynchronous operation) and having 500K documents in the Couchbase cluster it really small size. You should see under 1 millisecond get operations.

But consider having minimum 3 Couchbase nodes in one cluster, so that you can take one node down at any given point of time without compromising data stored in the cluster. See Sizing a Couchbase Server 2.0 cluster

Some additional resources:

  • 10 things developers should know about Couchbase
  • Top 10 things an Ops / Sys admin must know about Couchbase
  • App Development with Documents, their Schemas and Relationships
  • Couchbase Models
like image 90
user1697575 Avatar answered Sep 30 '22 12:09

user1697575