Open MV10 opened 4 years ago
Migrated comment by David Christensen on 2019-12-05 10:05:32 AM
Hi. I'm struggling to get my head around some of the Orleans concepts. In particular how you can initialise a grain with state. In your cache code SetAsync has
return clusterClient.GetGrain<iorleansdistributedcachegrain<byte[]>>(key).Set(new Immutable<byte[]>(value), TimeSpan.FromSeconds(seconds ?? 0));
Surely there is a race possibility here: After GetGrain has returned, but before Set has executed, there is a grain in the cache corresponding to key which does not have data associated with it. Which feels wrong.
Migrated comment by MV10 on 2019-12-05 2:34:42 PM
Good point.
Yes, if you have two clients trying to read the same value, there is a very narrow window where client A creates the grain and client B might read it before A can set a value.
But you also have to consider the possibility that client A hasn't even started doing work yet, and client B tries to read that same cache entry. Orleans will spin up the grain and serve a null value. From the client B perspective, it's exactly the same scenario as the race condition you describe.
I don't think this is likely to be a problem in most distributed caching scenarios I've seen, which tend to focus on things like user-specific values -- the reader of the cache is also the writer. However, it could be relevant if you're using the cache for sharing things like system-wide configuration values. In that case, I'd probably design some kind of read-wrapper that has a retry policy with exponential back-off.
Migrated comment by Assil on 2019-12-27 2:56:02 PM
Thank you for this nice article.
Yes, Orleans is a very good candidate technology for a distributed cache, because it is very powerful when it comes to distributed and concurrent systems and that is exacltly what caching is... Have you done and any performance comparison between Orleans cache and Redis for instance?
Do I need to worry about performance when it comes to large objects?
Let's forget about the caching policies for now and eviction.
Migrated comment by MV10 on 2019-12-27 5:22:09 PM
No, I haven't done any comparisons. Honestly most of the line-of-business apps I work with aren't particularly performance-sensitive, I'm usually more concerned with scale-out under load (a scenario where Orleans excels). Either option would be extremely sensitive to a wide variety of configuration options, so I think drawing useful conclusions from a comparison would be difficult. Generally speaking, you should try to avoid caching large objects regardless of the cache implementation (I try to cache more basic data like data PKs), but at the end of the day they're both persisting byte arrays, so my guess is results would probably be similar.
Written on 2019-09-18 11:40:06 AM
URL: https://mcguirev10.com/2019/09/18/distributed-caching-with-microsoft-orleans.html