C # Writing high concurrency database control

Source: Internet
Author: User

Often big data volume, high concurrency, bottlenecks are in the database, many people say with the database replication, publishing, read and write separation technology, However, there is a delay in synchronization between master and slave databases. The purpose of the code is to ensure that at the 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 already control the number of according requests in single digit.
However, an architect, if you can use a 99.99% security scheme, there is absolutely no 99.9%. So.

public static Long Currentvalidsessionid = -1;public static Object Databasedoor = new Object (); void Readdatabase (Request c Urrentrequest) {//Use Currentvalidsessionid to filter out other requests came in during the Execute time gap if (CU Rrentvalidsessionid = =-1) {//Use Object-lock to ' filter out ' other requests came ' during the variable change        Time Gap.            Lock (Databasedoor) {//Now there are only very little number of requests can reach below codes.                if (Currentvalidsessionid = =-1) {//Now there'll be is 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}}}

  

C # Writing high concurrency database control

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.