"High Concurrency Architecture Knowledge" High concurrency database access control code

Source: Internet
Author: User
Tags access database

public static long Currentvalidsessionid = -1;public static Object Databasedoor = new Object (); void Readdatabase (Request currentr Equest) {//Use Currentvalidsessionid to filter out other requests came in during the Execute time gapif (Currentvalidsessi Onid = =-1) {//Use Object-lock to ' filter out ' requests came in during ' variable Change time gap.lock (Databasedoor ) {//Now there are only very little number of requests can reach below codes.if (Currentvalidsessionid = =-1) {//Now the Re would be is only one request can access the Databasecurrentvalidsessionid = Currentrequest.sessionid;}}}  if (Currentvalidsessionid = = Currentrequest.sessionid) {//Here's the one! try{//Use transaction to guarantee the Execute time to void block//Access database codes go Here}catch () {//exception codes go here}finally{currentvalidsession  ID =-1; Recover to original State}}} 



The role of code is to ensure that the upper end of the cache service failure (generally the probability is relatively low), the formation of an inverted bottleneck, so as to protect the database, the database is down, is the big problem (such as affecting other applications).


Assumptions (not exactly the correct data, only examples):
Support 10,000,000 queries per second (tens of thousands of times);
One read library takes time: 1ms;
Modifying memory variables takes time: 0.001ms;
So:
The number of requests to the database that is last accessed per second < 1000
The other 9,900,000 requests are returned to other pages. That's why a lot of grab-list sites are accessible to someone who gets busy in the page.


Microscopic to 1ms, in the Currentvalidsessionid = =-1 of the time is 1ms, thus an average of 10,000 records influx.
Currentvalidsessionid from 1 to another value in 0.001ms, this time,


Lock (Databasedoor)
{
Now there are only one request can reach below codes.
if (Currentvalidsessionid = =-1)
{
Currentvalidsessionid = Currentrequest.sessionid;
}
}

An average of 10000x0.001=10 records will be executed to the above code, and the operating system will form a wait sequence for the lock.
So our goal is to allow only one read library per millisecond (because other apps will use it too), so we just want this to go into the 10, and eventually only one can move on.
So this is
if (Currentvalidsessionid = =-1)
{
}
The role of the. Once again, a request to the Atomic Protection queue is made, and only one can continue.


A little thought:
In fact, for a server with a frequency of N GHz, one memory is assigned to another memory data is the 1~4 instruction (average 2, two MOV operation), that is, 2/n ns time, not our hypothesis 1000ns (0.001ms). In fact, without atoms, we can control the number of TENS requests per second in single digit.
However, an architect, if you can use a 99.99% security scheme, there is absolutely no 99.9%. So.


"High Concurrency Architecture Knowledge" High concurrency database access control code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.