ASP. NET solves high concurrency scenarios

Source: Internet
Author: User
Tags date time stamp

Well, recently I saw a man jutting. How to deal with high concurrency. Actually chatting to the server matrix. I had not yet returned to God, after thinking, the server matrix I also know that mouth, but small and medium-sized enterprises can afford to play? As a programmer, many times you can only use the resources at hand to make optimization plans. (Life philosophy: Be wary of talkers)

I have collected the following online processing methods listed here. Although I will not be bored to the back down to bluff new, but deepen the image, there is a compendium is good.

Two big points:

Handle high concurrency through the server

Adjusts the maximum number of connections in the server application pool.

1. Adjusting the IIS 7 application pool queue Length

Changed from the original default of 1000 to 65535.

IIS Manager > ApplicationPools > Advanced Settings

Queue length:65535

2. Adjust the Appconcurrentrequestlimit settings for IIS 7

Changed from the original default of 5000 to 100000.

Appcmd.exe Set config/section:serverruntime/appconcurrentrequestlimit:100000

You can view this setting in%systemroot%/system32/inetsrv/config/applicationhost.config.

3. Adjust the settings of the Processmodel>requestqueuelimit in Machine.config

Changed from the original default of 5000 to 100000.

<configuration>
<system.web>
<processmodel requestqueuelimit= "100000"/>

4. Modify the registry to adjust the number of simultaneous TCPIP connections supported by IIS 7

Changed from the original default of 5000 to 100000.

REG ADD hklm/system/currentcontrolset/services/http/parameters/v maxconnections/t reg_dword/d 1000000

By completing the 4 settings above, you can support 100,000 simultaneous requests

See http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html

The maximum number of connections can also be adjusted for the DB server to be optimized.

After tuning the maximum number of connections, only the hardware and software load is balanced. Hardware load balancing can be realized directly through the intelligent switch, the processing ability is strong, and the system is independent, but the price is expensive, the configuration is difficult, can not distinguish between the internship system and application status. So the hardware load balancer is suitable for a large number of devices, large traffic, simple application. Software load balancing is based on system and application, and can better distribute load according to the condition of system and application. Good value for money. PCL load balancer software, LVS software under Linux.

concurrency control at the program level:

When two users access one page at the same time, one user may update the event that another user has deleted the record. Or, in the time between a user loading a page and clicking the Delete button, another user modifies the contents of the record.

concurrency control in ADO and Visual Studio. NET

Because data structures are based on disconnected data, both ADO and Visual Studio. NET use optimistic concurrency. Therefore, you need to add business logic to take advantage of optimistic concurrency to solve the problem.

If you choose to use optimistic concurrency, there are two general ways to determine whether a change has occurred: The version method (actual version number or date timestamp), and the Save all Values method.

Version number method

In the version number method, the record to be updated must have a column that contains a date timestamp or version number. When the record is read, the date timestamp or version number is saved on the client. The value is then partially updated.

One way to handle concurrency is to update only if the value in the WHERE clause matches the value on the record. The SQL representation of the method is:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE Datetimestamp = @origDateTimeStamp

Alternatively, you can use the version number to compare:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE RowVersion = @origRowVersionValue

If the date time stamp or version number matches, the record in the data store has not been changed, and the record can be safely updated with the new value in the data set. If they do not match, an error is returned. You can write code that implements this form of concurrency check in Visual Studio. NET. You must also write code to respond to any update conflicts. To ensure that the date timestamp or version number is accurate, you need to set a trigger on the table to update the date timestamp or version number when changes to the row occur.

Save All Values Method

An alternative to using a date timestamp or version number is to get a copy of all the fields when the record is read. The DataSet object in ADO maintains two versions of each modified record: The initial version (the version originally read from the data source) and the modified version (indicating user updates). When you try to write a record back to the data source, the initial value in the data row is compared to the record in the data source. If they match, it indicates that the database record has not been changed since it was read. In this case, the changed values in the dataset are successfully written to the database.

For the four commands of the data adapter (DELETE, INSERT, SELECT, and UPDATE), each command has a collection of parameters. Each command has parameters for the initial value and the current value (or modified value).

Note because the initial record does not exist, adding a new record (the INSERT command) requires only the current value, and removing the record (the delete command) only needs to use the initial value to locate the record to be deleted.

The following example displays the command text for a dataset command that updates a typical customer table. This command is specified for dynamic SQL and optimistic concurrency.

UPDATE Customers SET CustomerID = @currCustomerID, CompanyName = @currCompanyName, ContactName = @currContactName, contact  Title = currcontacttitle, Address = @currAddress, city = @currCity, PostalCode = @currPostalCode, Phone = @currPhone, Fax =  @currFaxWHERE (CustomerID = @origCustomerID) and (address = @origAddress OR @origAddress is null and address is null) and (city = @origCity OR @origCity are null and city is null) and (CompanyName = @origCompanyName OR @origCompanyName is null and CompanyName is null) and (ContactName = @origContactNa  Me or @origContactName is null and ContactName are null) and (ContactTitle = @origContactTitle OR @origContactTitle is null and ContactTitle is null) and (fax = @origFax or @origFax are null and fax is null) and (Phone = @origPhone or @origPhone I S null and Phone is null) and (PostalCode = @origPostalCode OR @origPostalCode are null and PostalCode is null); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City,postalcode, Phone, Faxfrom CUstomers WHERE (CustomerID = @currCustomerID) 

Note that the nine SET statement parameters represent the current values that will be written to the database, while the nine WHERE statement parameters represent the initial values used to locate the initial record.

The first nine SET statement parameters correspond to the first nine parameters in the parameter collection. These parameters set their sourceversion property to current.

The next nine WHERE statement arguments are used for optimistic concurrency. These placeholders correspond to the next nine parameters in the parameter collection, each of which sets its SourceVersion property to Original.

The SELECT statement is used to refresh the dataset after an update occurs. It is generated when you set the Refresh Data set option in the Advanced SQL Build Options dialog box.

Note that the above SQL uses named arguments, while the OleDbDataAdapter command uses a question mark (?) as the parameter placeholder.

By default, if you select the optimistic concurrency option in the Data Adapter Configuration Wizard, Visual Studio creates these parameters for you. The error handling code will be added by you according to your business requirements. ADO provides a DBConcurrencyException object that returns rows that violate the concurrency rule. For more information, see Handling concurrency Errors .

Handle high concurrency through programs

First, the cache.

System.Web.Caching.Cache Cache http://www.cnblogs.com/daizhj/archive/2007/08/15/855163.html

memcached Distributed Cache http://www.cnblogs.com/daizhj/archive/2009/03/23/1386652.html

Cache tiering (local cache +memcached) http://www.cnblogs.com/daizhj/archive/2009/11/17/1604436.html

Redis Architecture Design Http://www.cnblogs.com/daizhj/archive/2011/02/21/1959511.html

Llserver Architecture Design Http://www.cnblogs.com/daizhj/archive/2011/08/26/discuznt_llserver_arch.html

Cross-site cache synchronization http://www.cnblogs.com/daizhj/archive/2010/06/18/discuznt_memcache_syncdata.html

Tips

Memcached is a set of distributed memory object caching systems developed by danga.com (the technical team of operations LiveJournal) to reduce database load and improve performance in dynamic systems. The specific introduction can refer to:

memcached Depth Analysis
Http://www.cnblogs.com/luluping/archive/2009/01/14/1375456.html

Redis
Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.

http://doc.redisfans.com/

Http://www.cnblogs.com/shanyou/archive/2012/01/28/2330451.html

Second, static files are arranged separately

1. Transfer the user-uploaded attachment to another server via FTP. Http://www.cnblogs.com/daizhj/archive/2008/07/28/1254648.html

2. Distributing the static file cache through Squid
Using squid as a static front end, the most static files in the forum are sent or chained to a new HTTP link, which reduces the pressure on the Web server and improves performance.

Http://www.cnblogs.com/daizhj/archive/2010/06/10/1692758.html

Third, load balancing

Through the above scenario, the Web server pressure is small, performance is also improved, but if you encounter higher concurrent traffic, a single Web server can not meet the needs of a load-balanced scenario.

The use of lvs+keepalived, Nginx and so on. Related articles are as follows:

discuz! NT Load Balancer solution (HA)---LVS (Linux Virtual Server)
Http://www.cnblogs.com/daizhj/archive/2010/06/13/1693673.html

discuz! NT Load Balancer solution (HA)---LVS (Linux Virtual Server)

Http://www.cnblogs.com/daizhj/archive/2010/06/13/1693673.html

discuz! NT Load Balancing Scheme

Http://www.cnblogs.com/daizhj/archive/2010/06/24/1667422.html

The use of Nginx, using Nginx as the front-end load balancing, this is really attractive, there is time to try the next good.

Iv. Easing database pressure

discuz! NT database reading and writing separation scheme

Http://www.cnblogs.com/daizhj/archive/2010/06/21/dbsnap_master_slave_database.html

Full-Text Search scenario:

discuz! Sphinx Full-Text Search for NT Enterprise Edition (top)

Http://www.cnblogs.com/daizhj/archive/2010/06/28/discuznt_entlib_sphinx_one.html

discuz! Sphinx Full-Text Search for NT Enterprise Edition (bottom)

Http://www.cnblogs.com/daizhj/archive/2010/06/30/discuznt_entlib_sphinx_two.html

Working with large data volumes:

discuz! NT TENS data volume on the two carriages--tokyocabinet,mongodb

Http://www.cnblogs.com/daizhj/archive/2010/07/22/1781140.html

The knowledge that needs to be mastered

Memcached, Redis, Llserver, SQUID, NGINX, LVS, Sphinx

There is a version of Windows, there are also Linux versions.

In the absence of succession, programmers should be based on needs and current conditions.

Locally tested high-concurrency tools

Test method:
Local simulation test Site high Access high concurrency the test tool used is the famous LoadRunner, which is commonly known as a tool for testing. In De Shenjun's blog, there are several descriptions of stress concurrency testing through LoadRunner.

When Discuznt met the LoadRunner (on)
Http://www.cnblogs.com/daizhj/archive/2009/09/25/1573926.html

When Discuznt met LoadRunner (middle)
Http://www.cnblogs.com/daizhj/archive/2009/09/27/1574897.html

When Discuznt met LoadRunner (next)
Http://www.cnblogs.com/daizhj/archive/2009/09/27/1575091.html

ASP. NET solves high concurrency scenarios

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.