• 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

    • Here's my scenario: I have an intel i5-10600 based system using a Gigabyte Z590 Aorus master motherboard with an EVGA GeForce GTX 1070 (non-TI) graphics card. The PC has three monitors, 2 x Dell SE2717H/HX and an older Samsung monitor. The monitor configuration is Dell SE2717H - Samsung, Dell SE2717H. (1) The left Dell monitor is connected to the 1070 by via DP out on the 1070 (DP to DVI adapter connecting DVI to HDMI cable) going to HDMI input on the monitor. (2) The center Samsung is connected to the 1070 via HDMI output on the 1070 to HDMI input on the monitor itself. However I have a HDMI duplicator connecting the Samsung monitor to a NanoKVM box. Will explain this more in a moment. (3) The right Dell monitor is connected via a DP to HDMI cable. It's connected to DP out on the 1070 and HDMI input on the monitor. When the PC is powered on, the right (3) monitor is the default monitor showing the BIOS and POST screens before booting into Windows where the center Samsung becomes the primary monitor. I have the Samsung set as the primary in Windows, but that obviously doesn't affect the BIOS/POST. I had swapped the right Dell and center Samsung thinking the monitors were dictating the settings, but when I reconnected them, the Samsung was on the right, it still remained the priority monitor during BIOS/POST time. So I reverted those changes. I followed the EVGA card port priority per this official EVGA page ( https://www.evga.com/support/faq/FAQdetails.aspx?faqid=59560 ) which indicates the following priority of ports: - DVI - HDMI - DisplayPort (middle) - DisplayPort (far right) - DisplayPort (far left) With the DVI port not in use, I verified the center Samsung monitor was on the HDMI port on the 1070. The left Dell monitor was connected to DP (middle) and the right Dell monitor was connected to DP (far right). However during BOOT/POST, the right Dell remained the priority. I created all sorts of combinations of moving the center Samsung to the middle DP port (using DP to HDMI adapter), for the higher priority # and moving the left and right Dell monitors to far right and far left DP ports. However the right Dell monitor remained the BOOT/BIOS priority default. Why does this even matter?? The reason I'm trying to solve this puzzle is because I recently purchased a NanoKVM PCIe card that gives full BMC access to the BIOS and visuals via HTML5 web interface to pre-Windows boot time. In the event I need to troubleshoot when I'm away from home. The NanoKVM has an HDMI input port so it can grab the screen and share it via the HTML5 built in portal on it's remote IP access. Because the NanoKVM isn't multi-monitor aware, it's designed to only display the monitor that it's plugged into in the video card so I can see the  boot /BIOS screens and the Windows default monitor. Because there is no HDMI passthrough on the NanoKVM, I purchased a HDMI duplicator/splitter (NOT an extender).   This HDMI extender connects to the HDMI output on the 1070. My center Samsung monitor is connected to splitter output port #1 with a standard HDMI cable and the NanoKVM is connected to splitter output port #2 with a standard HDMI cable as well. When I disconnect both Dell monitors and leave this setup connected to HDMI, it works flawlessly. When I connect the left Dell monitor to the above far left DP port on the 1070, the setup still works great as the center Samsung still remains the default/priority. But when I connect the right Dell monitor to the far right DP on the 1070, the right Dell becomes the default/priority monitor during BOOT/POST time. This results in the NanoKVM not having access to/displaying the contents of the right Dell when it's priority during BOOT time, but once the PC boots into Windows and it's the default monitor/port, the KVM displays that just fine then. However, this defeats the purpose of needing the KVM which is to provide access to the BIOS and boot menu settings in the event of an emergency while I'm away from home. Any idea on what I'm doing wrong? Do I need to replace the center Samsung?? Different monitor / adapter cables??? Do I need to upgrade the video card? I have a 980Ti, but per the EVGA docs, the port priority is the same for it as the 1070. Thoughts?? Suggestions???
    • I've had the Ryzen since it first went to market. I've had Windows 11 since beta testing it in the early days... Ryzen worked just fine, Windows didn't... so, not sure what this "four years" it is you're referencing since you're not specific as to who fixed what.
    • A toggle option for automatic call recording will be welcomed. I see no harm in providing it as the call recording prompt is played by the iphone.
  • Recent Achievements

    • Mentor
      Karlston went up a rank
      Mentor
    • One Month Later
      EdwardFranciscoVilla earned a badge
      One Month Later
    • One Month Later
      MoyaM earned a badge
      One Month Later
    • One Month Later
      qology earned a badge
      One Month Later
    • One Year In
      Frinco90 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      474
    2. 2
      snowy owl
      256
    3. 3
      +FloatingFatMan
      254
    4. 4
      ATLien_0
      210
    5. 5
      Xenon
      149
  • Tell a friend

    Love Neowin? Tell a friend!