Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Synchronisation in Multi Server environment

Synchronisation in Multi Server environment

Scheduled Pinned Locked Moved C#
questiondatabasesysadminbusinessjson
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    c0der2009
    wrote on last edited by
    #1

    I have a process which can be run from any number of servers. All of them access the same database. How do I synchronize the severs data access? Here are the details. My common database server has a table T1. It has columns C1, C2 and C3. My process has the following steps. 1. Read T1 table. 2. If there is any row with value V1 for C1, wait in a loop and go to step-1. 3. If there are no such rows, insert a row with V1 for C1 and appropriate values for the rest of the columns. 4. Start my business logic. 5. Once the business logic is complete, delete the row entered in step-3. Now the same process runs from different servers. So there is a chance that 2 or more processes will do steps 1 and 2 simultaneously and might end up creating 2 or more rows with V1 value for C1. I don't want that to happen. How will I make sure only once process can perform steps 1 and 2 at a time? I can not use lock and monitor to synchronize the access as I am running from different process spaces. How do I do it then?

    J 1 Reply Last reply
    0
    • C c0der2009

      I have a process which can be run from any number of servers. All of them access the same database. How do I synchronize the severs data access? Here are the details. My common database server has a table T1. It has columns C1, C2 and C3. My process has the following steps. 1. Read T1 table. 2. If there is any row with value V1 for C1, wait in a loop and go to step-1. 3. If there are no such rows, insert a row with V1 for C1 and appropriate values for the rest of the columns. 4. Start my business logic. 5. Once the business logic is complete, delete the row entered in step-3. Now the same process runs from different servers. So there is a chance that 2 or more processes will do steps 1 and 2 simultaneously and might end up creating 2 or more rows with V1 value for C1. I don't want that to happen. How will I make sure only once process can perform steps 1 and 2 at a time? I can not use lock and monitor to synchronize the access as I am running from different process spaces. How do I do it then?

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      One way would be to create a separate table in the DB for locks (or just reuse the existing one). Before a process starts step 3 (or whatever the crucial step is) it checks that table for a well known value, if it exists then it knows that some other process has started business logic and it should wait. If the lock value isn't found then it can create it and continue on. The lock value that it created will signal other processes that it is currently executing business logic and they should wait. This will work if the DB column that you're using for a lock is properly setup with a primary key constraint. If two processes try to write the same value into a PK column at (nearly) the same time the first will succeed and the other will throw an exception. Another "Outside the Database" solution would be to make all of the processes communicate with each other via some other mechanism (sockets, MSMQ, File IO, etc) and work out that way which one should be doing what.

      C 1 Reply Last reply
      0
      • J Jimmanuel

        One way would be to create a separate table in the DB for locks (or just reuse the existing one). Before a process starts step 3 (or whatever the crucial step is) it checks that table for a well known value, if it exists then it knows that some other process has started business logic and it should wait. If the lock value isn't found then it can create it and continue on. The lock value that it created will signal other processes that it is currently executing business logic and they should wait. This will work if the DB column that you're using for a lock is properly setup with a primary key constraint. If two processes try to write the same value into a PK column at (nearly) the same time the first will succeed and the other will throw an exception. Another "Outside the Database" solution would be to make all of the processes communicate with each other via some other mechanism (sockets, MSMQ, File IO, etc) and work out that way which one should be doing what.

        C Offline
        C Offline
        c0der2009
        wrote on last edited by
        #3

        Thanks Jimmanuel for the reply. It gives some good pointers. I am more interested in the "Outside the Database" solutions. I suppose there are no design patterns as such which cover this scenario. Right?

        J 1 Reply Last reply
        0
        • C c0der2009

          Thanks Jimmanuel for the reply. It gives some good pointers. I am more interested in the "Outside the Database" solutions. I suppose there are no design patterns as such which cover this scenario. Right?

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          Sure there are, look up "concurrency[^]", "transactions[^]", "inter-process communication[^]" (aka IPC) and "messaging systems[^]". I haven't looked for the others but I know that there's a decent IPC article on CP somewhere. If you have lots of time to read, this[^] book has a chapter about transactions and concurrency and this[^] is entirely about building applications and messaging protocols in distributed systems. :badger:

          C 1 Reply Last reply
          0
          • J Jimmanuel

            Sure there are, look up "concurrency[^]", "transactions[^]", "inter-process communication[^]" (aka IPC) and "messaging systems[^]". I haven't looked for the others but I know that there's a decent IPC article on CP somewhere. If you have lots of time to read, this[^] book has a chapter about transactions and concurrency and this[^] is entirely about building applications and messaging protocols in distributed systems. :badger:

            C Offline
            C Offline
            c0der2009
            wrote on last edited by
            #5

            Thank you very much. Really helpful leads.

            J 1 Reply Last reply
            0
            • C c0der2009

              Thank you very much. Really helpful leads.

              J Offline
              J Offline
              Jimmanuel
              wrote on last edited by
              #6

              Glad to help :) :badger:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups