Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate second-level cache using Redis vs just using Redis

My scenario is I prefer to stay in a relational database storage system like SQL Server because I would need to work with complex queries.

And then, because some calculations would be better to be done overtime and just store the results into something like Redis or maybe a more traditional NoSQL solution.

That's the point where I thought: and what happened with the second-level cache on NHibernate?.

I did a very small research and I found that there's a Redis second-level cache provider, and now I got "confused".

I mean, if I use NHibernate's second-level cache most of object access should be very fast as it should be no database roundtrip, thus most accessed objects would be retrieved from the in-memory Redis store.

Why I'm considering this instead of just using Redis directly? Because I need actual atomic transactions within my solution's domains.

Ok, the question?

Is relying on NHibernate's second-level cache Redis provider a good idea in order to get the best of relational and schema-less worlds?

What's your advice?

like image 861
Matías Fidemraizer Avatar asked Feb 15 '23 04:02

Matías Fidemraizer


1 Answers

I see two different things as a summary of your view:

  1. Use redis as second level cache above NHB. This make perfect sense as SLC stores separated fields of objects and redis is key/value store. As I remember, SLC contain results of scalar queries or mapped and fetched objects but what's important, the data are taken (cached) from performed queries.

    IMHO if you would use redis this way, all cached values must result from NHB queries. This brings you some kind of transaction atomicity, how did you already described, but as far as I know, we found couple bugs when SCL returned stale data or data from uncommitted transactions.

    Note that this approach says that someone (NHB) still needs to somehow guarantee business transaction between RDBMS and Redis, which is not simple and buggy.

    Also note that SLC itself is not incredibly fast pattern. As SLC contain field of object and not object itself, every hit results into new object creation. So what happen is fetching data from Redis instead of resultset obtained from executed SQL query. So, when you use prepared statements and RDBMS typically makes caching for you, you can find out that this does not bring very large performance improvement for you.

  2. Redis as a separated business store. You managed data completely on you own, you can make their computation within native (C#) code (contrary to SQL query or mapped object). You need to guarantee fresh data and some transaction approach.

What I would choose? Separated redis. Why?

  1. Second level cache along with mapping puts some contract to you as the content results from queries or mapped objects. You can't manage or use Redis on your own. Especially your cached data are coupled/tight to those queries, not to some API (interfaces) and some kind of service (as I would design it)
  2. You can make computation of you data in your own code.
  3. SLC approach seems buggy for me and often it was very hard to find these bugs.
like image 73
Martin Podval Avatar answered Feb 17 '23 21:02

Martin Podval