Asp.Net site performance optimization slow word decision (top) buffering write data

  • 2020-05-10 17:58:20
  • OfStack

You've read a lot about the Asp.Net cache. So I'm going to change my mind a little bit when I write, and I'm going to start with buffering the data. Buffered write data means that instead of saving the data to the database immediately when the data needs to be updated, buffered 1 and then written to the database at the appropriate time.

Buffered write data can avoid the situation of deadlock or untimely response caused by excessive pressure on the database when the website is accessed concurrently.

So when is a good time to write buffered? Is that true in all cases? Buffered writes cause the data to reside in memory or on web server hard disk or third party storage for a period of time during which it may be missing if the latest data is queried from the database. Most things have two sides, we need to learn to seek advantages and avoid disadvantages; In other words, a buffered write can be used either to ensure that a buffered write does not cause the user to feel missing data, or to prevent the user from feeling missing data with appropriate measures. I have two concrete examples of how to use buffer writing:

1. Pv(page view) statistics. Most websites have this feature, and some even specialize in this service
Every time a page is viewed, the Pv+1 of the corresponding page needs to be given. In this case, if the direct update to the database, a slightly larger number of visits will cause the database too much pressure.

Therefore, we need to buffer the Pv count. In the case of single web server, we can maintain one hashtable in memory, and then use an asynchronous thread to periodically scan the hashtable and update it to the database when the number of hits reaches "1 fixed number". It sounds simple, but it also requires a little trick. The four words "1 fixed number" mentioned in the last sentence cannot be just a random number. If it is 4, just imagine what will happen to 1. All our Pv Numbers will be multiples of 4. We didn't cheat but we left a sign of cheating! This "1 definite number" must be a prime number, we can take 7, we can take 13, we can take 23 or 31 if we have a lot of hits; There would be no "signs of fraud".

2. Send short messages on your site
SMS is also a common module, which can be said to be an offline message rather than an im instant message. Therefore, we can take advantage of this business feature to make a buffer for sending messages. When have short message is sent, we can put this short message in hard disk file first, and then quickly respond to user, on ui tell users, your message is sent, then we can use another one thread (or do one windows services) to monitor the buffer of short message catalog order will be a short message stored in the database.

Above two scenarios are more classic examples of use of buffer to write, in reality we need to go to the specific analysis of the business and whether or not a business pressure will cause the database to determine whether the buffer to write, if the business itself is very small, the pressure of the database that is, of course, there is no need to consider, if business pressure we need to do more work to avoid the problem of buffer to write and use the buffer to write.

Please respect the author's labor, reprint please keep the link to the technology blog

Related articles: