• 0

C# IDistributedCache Multi-Thread Approach Question


Question

I have a project that users send data to a server written in C#. The users have the ability to send an action against an object. To reduce DB Traffic, I am using IDistributedCache to hold the instance of the main object, as it also allows for cross-server shared caching.

I am running into an issue with multiple threads where two people send an action to the server. Each thread calls the same function which loads up the instance of the object from the IDistributedCache (using _cache.getAsync(,....)).  This results in each thread effectively ending up with it's own instance of the object.  The issue is that whatever thread finished last, ends up writing that instance of the object back to the cache, thus losing any changes made in the other thread as the modified object isn't what the current thread has in it's memory.

 

I was thinking that I could use a standard hashtable/dictionary and just reference the object directly ie: objdict[objid].actions.add(action). That way I have one instance in memory that all threads would have the same state for. However, doing it that way I have no way to support distribution like I do with the IDistributedCache.

 

I am wondering if anyone has dealt with this, or has any idea of how can I make it so that both threads get the same reference to the object (akin to using the dictionary), but keep it so it can be split across servers (distributed). 

3 answers to this question

Recommended Posts

  • 0

I worked on a project a few years ago where we had a similar scenario. We had similar issues and many solutions put forward for tests. Included queuing the object instance between threads so that the instance was ONLY active in one thread.

The final solution though, didnt go this route, but what was done was to take the principal of a shared whiteboard. By this i mean each thread gets a new instance of the 'core' instance, but with triggers to each instance to push through from the child instance to the core, and from the core push those changes to each subsequent child.

This solution worked but did also suffer from the same race condition, but on a smaller basis, that was resolved by discarding any changes to the sub-properties at the lowest level on a rule of Who Makes Changes First keeps them.

 

Don't think this will help you much, but i hope it helps give you an idea...

  • 0
  On 22/03/2019 at 12:51, Jonathans said:

I worked on a project a few years ago where we had a similar scenario.

Expand  

Also we were using a state engine of sorts, not IDistributedCache, but principal was similar

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The recent iOS (and macOS) updates feel more like incremental releases than major updates. Relatively modest changes and used as an excuse for dropping support for older devices. Although it does feel like iOS 26 might be visually very different, perhaps to take media attention away from Apple Intelligence problems.
    • There is no one at home at the top and is a straight out lunatic so this is not very surprising at all. My moral would be shot rolling crap out like the cybertruck.
    • I hope this is also rolling out to the website so don't have to install the mobile app to set privacy settings. WTF?
    • Not having a native app I guess is not good. That said when I have to use Windows and I want to use AI I do not use co-pilot. Lots of AI apps work on lots of OS’es. Just install the AI app you want on iOS or MacOs. Also we are in early days. In 3 years time I suspect lots of these AI apps will be gone either by not making it or being bought up by others.
    • Gemini can now catch you up on the latest changes to your files in Google Drive by David Uzondu Google recently announced a new feature for Drive designed to help you keep track of what has changed in your files without having to dig through them manually. The feature, called "Catch me up", shares its name with a similar tool in Google Chat, which helps users quickly catch up on conversations without scrolling through long message threads. With the new feature, Gemini will now attempt to identify files in your Drive that have been altered since you last opened them, offering a summary of these changes. This aims to save you the headache of figuring out what is new or crucial. This covers edits made to Docs and also new comments left on Docs, Sheets, and Slides. Google clarifies that this summary is not exhaustive but is meant to point out potentially important updates. To use it, you can find a "Catch me up" prompt on the Google Drive Home page, and clicking it will bring up the summary in the Gemini side panel. Alternatively, for individual files, you will see an activity indicator on documents that have likely changed. You can click this indicator directly or select the file and hit a "catch me up" button in the selection toolbar to get the lowdown. The feature is currently available in English and has begun rolling out to users on both Rapid Release and Scheduled Release domains. It could take up to 15 days for everyone eligible to see it. It is coming to Google Workspace accounts, including Business Standard and Plus, Enterprise Standard and Plus, as well as customers with a Gemini Education or Gemini Education Premium add-on, and Google One AI Premium subscribers. Users who previously bought the Gemini Business or Gemini Enterprise add-ons before those were discontinued for new sales on January 15, 2025, will also get access.
  • Recent Achievements

    • Week One Done
      mywakehealth earned a badge
      Week One Done
    • Dedicated
      jbatch earned a badge
      Dedicated
    • Week One Done
      Leonard grant earned a badge
      Week One Done
    • One Month Later
      portacnb1 earned a badge
      One Month Later
    • Week One Done
      portacnb1 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      284
    2. 2
      snowy owl
      158
    3. 3
      +FloatingFatMan
      149
    4. 4
      ATLien_0
      138
    5. 5
      Xenon
      130
  • Tell a friend

    Love Neowin? Tell a friend!