• 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

    • "Can" is fine. I want the ability for it to "not." Actually, I would prefer vice versa. But it has to be optional, otherwise companies wouldn't be too happy.
    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      331
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!