Spike high concurrency solution of

First, what is high concurrency

 

High concurrency (High Concurrency) is one of the factors the Internet distributed system architecture design must be considered, it usually means that can simultaneously handle many concurrent requests through the design assurance system. Some commonly used high concurrency-related metrics are response time (Response Time), throughput (Throughput), query rate per QPS (Query Per Second), the number of concurrent users.

Response Time : system time response to requests. For example, a system for processing HTTP requests needs to 200ms, the system response time is 200ms.

Throughput : the number of requests processed per unit time.

QPS : in response to requests per second. In the Internet field, this indicator and throughput distinction is not so clear.

Concurrent users : the number of users while carrying the normal use of the system functions. For example, an instant communication system, and represents the number of concurrent users of the online system amount to some extent.

Second, what is the spike

Spike scenes usually held a number of activities in the electricity business website or holiday encounters grab votes on the 12306 website. For some scarce electricity supplier website or specials, General Electric's Web site will be limited sales at the appointed time, because of the special nature of these commodities, will attract a lot of customers come to buy, and will at the same time at the agreed point in time pages spike rush.

Such a scenario is very characteristic of high concurrency scenarios, if not reasonable traffic management and control, the impact of wanton indulgence large flow system, it will lead to a series of problems, such as the number of available connection resources are exhausted, the distributed cache capacity It is explode, reduced throughput database, would ultimately cause the system to produce an avalanche effect.

In general, the practice of large-scale Internet sites are usually separated by expansion, movement, caching, five kinds of service degradation and limiting conventional means to protect the stable operation of the system.

Third, how to solve the high concurrent spike

1. limiting

Before discussing why limiting our first live chat limiting those scenes can be seen everywhere.

Such as rush hour, subway stations influx of large numbers of people, can cause severe congestion, it had just spent the most time of about five minutes from the station hall to the site, but was forced to take 30 minutes or more to be limiting in control smoothly into the platform.

Spike high concurrency solution of

 

Spike high concurrency solution of

 

After the above two pictures on the display of a crowded subway scenario, if all the people all the influx of the site, it will cause more people can not get on the train, so take control, we can get people through the concourse level of the ground and double waiting in line, to reduce the pressure of the site, to ensure that every passenger can finally smoothly on the train.

In the spike electricity supplier system, there will be a mass influx of users simultaneously, as only a small part of the user to spike success, so most of the traffic restrictions, allowing only a small part of the flow into the back-end service.

Limiting server can be used to limit the number of connections waiting and waiting time, a time release small number of users, so that more users in a fake queued state. For example tomcat configuration:

1<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxThreads="800" acceptCount="1000"/>

The last two parameters have the following meanings:

the maxThreads : maximum number of threads tomcat start, i.e., the number of concurrent processing tasks, the default value 200 is  acceptCount to : when the number of threads tomcat started when the maximum number of accepted requests queued, the default value is 100  how these two values from role, see the following three cases

Case 1: a request is received, then the number of threads does not reach the maxThreads start tomcat, tomcat will start a thread to handle this request.

Case 2: a request is received, then the number of threads has reached the maxThreads start tomcat, tomcat this request in a queue will wait idle threads.

Case 3: a request is received, then the number of threads has reached the maxThreads tomcat start, the number of requests in the queue wait reached

acceptCount, this time tomcat directly reject the request, return connection refused

2. static pages

First, we can use Freemarker the page is static, allowing users to reduce interaction between with the back-end server. This will reduce the pressure on the server, if the conditions allow we can use CDN acceleration.

Freemarker principle in the following figure, templates + data can generate static pages by Freemarker.

Spike high concurrency solution of

 

 

CDN is a source station to the content distribution node closest to the user, so the user can conveniently obtain desired content, improve the response speed and the success rate of user access. For example the following figure: Beijing Internet users will automatically have access to the resources nearest and fastest server.

Spike high concurrency solution of

 

3. The introduction of Redis

Limiting static and are designed to reduce the pressure on the back-end server, but the end user's request or a server will fall, in order to increase the user experience, we should also accelerate the speed accordingly. The interaction between the back-end code will be reduced and databases corresponding speed, so we can use Redis to read data at high speed.

Redis is an extremely good level NoSql database memory, read and write speed up to 5w / s single thread. So we can use Redis in many cases to solve the problem of high-speed reading.

In particular stocks oversold, we can spike at the beginning, the total amount of inventory stored in Redis, whenever the user to buy, use of type String decr method to minus one, minus a success if, as the considered successful enough to grab, and the user and product information into order entry Redis, when the end finally snapped, and then we put together Redis stores order information into the database.

Reference Code:

1	# 设置总的待抢购数量为1000000
2	set amount 1000000;
3	# 抢购 数量减一
4	decr amount 5
6	# 把用户信息存入到抢购成功的集合中,由商品ID命名来区分
7	lpush 用户ID 商品ID

IV Summary

Can be solved by the above three schemes spike issues under most scenarios, of course, there will be more unexpected situation under high concurrency, then we can try more orientation for your business, resource scheduling, to find the most suitable own solution.

Published 682 original articles · won praise 1391 · Views 1.71 million +

Guess you like

Origin blog.csdn.net/itcast_cn/article/details/104865801
Recommended