The function of the code is to ensure that when the upper cache service fails (generally, the probability is relatively low), an inverse bottleneck is formed, so that the database can be protected. If the database fails, it is a big problem (such as affecting other applications).
Assumption (not exactly correct data, just for example):
Supports 10,000,000 queries per second (10 million);
It takes 1ms to read the library at one time;
Modifying memory variables takes time: 0.001ms;
So:
Number of requests to the database final access per second < 1000
The other 9,900,000 requests will be returned to other pages. This is why many order grabbing websites can be accessed, while others get pages in a busy manner.
From microscopic to 1ms, the time at currentValidSessionID == -1 is 1ms, so there will be an average of 10,000 records influx.
The time when the currentValidSessionID changes 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 = ;
}
}
On average, 10000×0.001=10 records will be executed to the above code, and the operating system will form a waiting sequence for the lock.
Then our goal is to only allow once a library read per millisecond (because other applications will also use it), so we only hope that this entry will only be one of the 10 items, and in the end it will only be able to continue moving forward.
Then this is
if (currentValidSessionID == -1)
{
}
The effect is. Make another judgment, and only one request to enter the atomic protection queue can continue.
A little thought:
In fact, for a server whose main frequency can be N GHz, one memory number assigned to another memory data is 1 to 4 instructions (2 on average, two MOV operations), that is, 2/N ns time, rather than the 1000ns (0.001ms) as we assumed above. In fact, without atoms, we can already control the number of accesses requested at hundreds of billions of dollars in single digits.
However, if an architect can use a 99.99% security solution, he will definitely not 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 = ;
}
}
}
if (currentValidSessionID == )
{ // 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
}
}
}
The above is all the content described in this article. I hope it will be helpful for everyone to learn high concurrency programming in C#.