c written by high concurrent database control access code

  • 2021-01-06 00:43:06
  • OfStack

The purpose of this code is to protect the database by creating an inverted bottleneck in the event that the upper cache service fails (usually with a low probability), while a database failure is a major problem (such as affecting other applications).

Hypothesis (not completely correct data, only for example) :
Support 10,000,000 queries per second (10 million);
Time required to read one library: 1ms;
Time elapsed to modify memory variables: 0.001ms;
So:
The number of requests to the database that are finally accessed per second < 1000
The other 99,900,000 requests are returned to other pages. This is why a lot of bidding sites have people visiting them, while others get busy pages.

In terms of 1ms, the time for currentValidSessionID == -1 is 1ms, so on average 10,000 records will flood in.
The time for currentValidSessionID to change from -1 to other values is 0.001ms. During this time,


  lock (databaseDoor)
  {
    // now there is only one request can reach below codes.
    if (currentValidSessionID == -1)
    {
      currentValidSessionID = currentRequest.SessionID;
    }
  }

On average 10000×0.001=10 records will be executed to the above code, and the operating system will form a wait sequence for the lock.
Our goal is to only allow one read per millisecond (because other applications will use it), so we only hope that of the 10 that come in, only one will move forward.
So this is


if (currentValidSessionID == -1)
{
}

The role of. Once again, only one request to enter the atomic protection queue can continue.

1 Points to think about:
In fact, for a server with a host frequency capable of running N GHz, one memory number assigned to another memory data is 1 to 4 instructions (average 2, 2 MOV operations), which is 2/N ns time, rather than 1000ns (0.001ms) as we assumed above. Instead of using atoms, we've been able to keep the number of access requests over 100 billion to single digits.
But an architect who can use a 99.99% safe solution will never use 99.9%. SO.


public static long currentValidSessionID = -1;
public static object databaseDoor = new object();
void readDatabase(Request currentRequest)
{
    // use currentValidSessionID to filter out other requests came in during the execute time gap
    if (currentValidSessionID == -1)
    {
        // use object-lock to filter out other requests came in during the variable change time gap.
        lock (databaseDoor)
        {
            // now there is only very little number of requests can reach below codes.
            if (currentValidSessionID == -1)
            {   // now there will be only one request can access the database
                currentValidSessionID = currentRequest.SessionID;
            }
        }
    }
    if (currentValidSessionID == currentRequest.SessionID)
    {   // here is the one !
        try
        {
            // use transaction to guarantee the execute time to void block
            // access database codes go here
        }
        catch()
        {
            // exception codes go here
        }
        finally
        {
            currentValidSessionID = -1;  // recover to original state
        }
    }
}

That's all for this article. I hope it will help you learn C# high concurrency programming.


Related articles: