Spike how the system design?

 

 

A longer article, went straight to the focus, please share with me.

 

What is a spike?


"Spike" is a means of business operations that promote a particular point in time, reflected in the system level, it refers to a Web system, the user receives tens of thousands of a second request to buy a limited number of promotional products. In essence, the spike system is a " three high " system, that is, high concurrency, high performance, highly available distributed systems. This article from the "high concurrency" perspective, to look at the main issues to be addressed.

 

Spike to deal with concurrent read, concurrent write. "Concurrent read" by all means necessary to reduce the user to the server to read the data, and the "concurrent write" The main need to ensure the correctness of the data to ensure consistency under the large flow of data.

 

Spike system focus on three aspects


Below an example of a spike system, take a look at three issues a major concern spike system.

 

A high concurrent access

Users continue to refresh the page before the spike view, to get the buy button, click the concurrent purchase when buying focus, the core is a high concurrent read access to the system data, the following can be carried out by means of protection.

 

Access interception : From the browser / multi-layer reverse proxy intercepts traffic / Web layer / server layer / data layer, try to visit intercept closer to the user layer, reduce the pressure on the background;

Shunt : distributed mainly through clustering technology, process multiple machines, improve concurrency;

Static and dynamic separation : the separation of possible dynamic data and static data, and data read and be accelerated by caching CDN techniques;

 

Second, the data correctness

Spike accompany this series of business processes, including browsing merchandise, buying into the page, purchase, inventory deductions, pay. Concurrent with which to write the major inventory-related deductions, to ensure the accuracy of the data, to prevent the occurrence of oversold. Probably solution are:

 

Less Stock : Analyzing transaction, into separate hot hot commodity database, to increase concurrency lock

Hot : hot core real-time data analysis, and optimization or isolation in the system

Asynchronized : it refers to the acceptance of a purchase request and handle asynchronous technology. First purchase request into the queue, this process is very efficient, return customer information.

Downgrade limiting : the request limit, the access control system; downstream dependent service downgrade, ensure that the core link

  

Third, the anti-cheating

Buying also need to ensure fairness, Ark tickets such as plug-ins to buy train tickets, scalpers and so on. In the technical point of view, probably methods are:

 

Answer : In order to increase the complexity of the purchase; delay request.

Cache check : check whether the cache purchased this item has been recorded when the user purchase request process.

 

System design principles spike


1, to minimize the number of requests reception

Reception will increase the burden browser request, try to simplify or merge the page size (CSS / JS, images, etc.), remove the page decoration; parsing reduce DNCS

 

2, background data as little as possible

Background data will increase network transmission, compression and character encoding, consumes CPU. We need to sort the data and services depend on the background, concern serialization / de-serialization way to reduce unnecessary data exchange

 

3, the path should be as short call

Distributed call complex, the need to shorten the call link; reduce reliance third party, try to rely on the weak, and to do well the rating

 

4, try not to have a single point

Availability and stability of the angle, eliminating single points; stateless of service, and a service state of decoupling the machine, such as dynamic machine configuration; the state of the memory, increase availability through redundancy.

 

High concurrency of "access block"


High concurrent access interception main idea is:

From the browser / reverse proxy / Web layer / server layer / multi-layer block traffic data layers, try to intercept access layer closer to the user, as much as possible to filter out invalid request , let the "funnel" the end is the most a valid request. Meanwhile, the user request is processed earlier, but also reduces the RT each request, thereby improving the degree of concurrent systems and QPS.


 

Browser access interception

Prevent users from unnecessary clicks, you can restrict the user's behavior at the product level, such as the click of a button is not available, for a limited time to accept the request. Of course, in order to prevent malicious buying, you also need to do other attempts at anti-cheating, such as talking about earlier answer / cache checking.

 

CDN acceleration

CDN stands for Content Delivery Network, ie, content delivery network. Simply put, is to copy the data on the original server to another server, users access the server nearest access to resources. The disadvantage is that the content changes to take effect slowly.

 

Reverse proxy access interception

Mainly static and dynamic separation, static files as part of the project's web publishing; another by Nginx hosting static files, Web services reduce the pressure.

 

Web Service layer and access layer interception

The main dynamic data access, caching strategies can be employed to reduce data access to the next level. Cache can be divided into local cache and redis, memcache and other caching middleware.

 

High concurrency of "diversion"


Shunt distributed mainly through clustering technology, process multiple machines, improve concurrency.


DNS Polling : from App / Browser inlet, classifying a request, static data directly to acquire CDN, while clustering of Nginx, DNCS by polling.

 

Load balancing Nginx : Nginx supports concurrent access of 10W, Nginx supports proxy policy configuration requests, route requests to multiple Web servers handle. Nginx supports load balancing strategies include: polling, weight, ip_hash, fair, url_hash etc.

 

Load distributed architecture strategy : Web layer calls service, as well as calls between the service, each service needs to deploy multiple copies. The two most commonly used framework technology, spring cloud, dubbo, HSF, using a client load balancing strategy, routed to different instances of service.

 

Redis cluster : redis single hundreds of thousands of QPS, by Redis cluster, assign data to multiple servers, reduce the load on each machine.

 

Mysql separate read and write : a write request does not generally split, can also be distributed database write request to the shunt technique. For a read request, the general use of separate read and write strategy. Reading Library applications set the index alone, improve read performance.

 

High concurrency of "static and dynamic separation"


Dynamic page : real-time data to render a page, you need to call back, slow read speed

Static pages : the file is stored in the file system, does not change often, read speed is very

In order to improve efficiency, as static, static pages, dynamic pages replacement. For example, product information pages, product information after the release of stored under static pages of product information; real-time data access to only the necessary data.

 

Several concerns:

1,   static data cache to the nearest place from the user. Such as the browser, CDN, service side of the Cache

2, can be directly cache  HTTP connection. The key is whether they contain personalized data and data related to visitors, such as whether the data output and URL, visitors, time, place-related, and whether it contains Cookie and other private data.

3, often using Nginx Web server and cache static files. Java system itself is not good at handling a large number of connection requests, and the need to try to cache Web side Nginx

4, the dynamic data as possible Json technology . Reduce unnecessary data transmission and parsing

5, the cache also need to consider failures / problems hit rate / post updates issue / movement data consolidation; to fail, for example, generally three ways: timeout updates, regular updates, the update notification.

 

Spike's "cut stock"


Less inventory generally as follows: single inventory reduction, inventory reduction payments, withholding stocks.

Among them, I would rather recommend " orders minus inventories " because to participate in spike, buy is to make, under the circumstances is not successful after a single payment of less; while sellers have strict limits on stocks of commodities spike, more simple logic .

 

Of course, "orders minus inventories", to ensure that inventory data can not be negative.

Some methods:

1, the transaction judgment , which is to ensure the reduced inventory can not be negative, otherwise it is rolled back;

2, provided directly database field data unsigned integer, directly error is less than zero;

3, hot commodity in a separate hot Library

4, the concurrent lock : locks increases concurrent, such as queuing

5, counters : a counter to make the Web Service layer or layers, technical threshold is reached, return directly snapped failure.

6, under the trade route : the route of buying the same category of goods to the same service and DB links, reduce unnecessary lock contention.

 

In practice some of the principles:

1. Web data is cached in the read side, the read filter out invalid data;

2. The read data is not stronger consistency check;

3. Based on the write data to make time slice, filtered expired requests;

4. write requests to do limit protection, overload filter request;

Strong writing data consistency check 5. Yes, only the last valid data validation.

 

Spike's "hot spots"


Spike hot core data is required for real-time analysis, roadmap can be optimized / limiting / isolating from the service / system / data for different angles , such as:

 

Static hot data : predict in advance. Sellers register, in advance marking

Dynamic number of hotspots data : hotspot system during operation temporarily produced

 

Separate hot spots can also increase the analysis component of the overall business focus control system, and make the corresponding limitations and isolation method.

 

Spike's "asynchronous"


Spike on the system design, often using message queues to buffer the instantaneous flow rate , the synchronous request calls into asynchronous push indirectly, through an intermediate queue receiving a peak instantaneous flow rate at one end, the consumer message smoothly at the other end.

 


In essence, it is to become a two-step by step, which add some mechanism to buffer. For example, in spike coupons to buy, grab a user code (to determine whether the user is eligible) and system development code (to qualify for users to distribute coupons), grab the code 10 may be tens of thousands of QPS, only you need to know whether there are qualified; but only the hair code for qualified users, QPS possible only a few thousand, using message queues, can greatly improve efficiency.

 

In addition to the message queue, the queue can also be used in other ways, such as locking the thread pool waits memory queuing algorithms (such as FIFO), queue files (such as those based synchronization of MySQL binlog)

 

Takeaways


  • Spike system is a "three high" system, that is, high concurrency, high performance and high availability of distributed systems

  • Spike design principles: Reception request as little as possible, as little background information, call link as short as possible, try not to have a single point

  •  Spike high concurrent methods: visit interception, diversion, static and dynamic separation

  • Data spike method: reduce inventory strategy, focus, asynchronous, limiting downgrade

  • Access to intercept the main idea: by CDN and caching technology, try to intercept access layer closer to the user, as much as possible to filter out invalid request.

  • Shunt main ideas: through distributed cluster technology, multiple machines treatment to improve concurrency.

 

At last


Spike high concurrency requirement of the system, both high performance and high availability requirements are set forth herein primarily from the perspective of high concurrency. For high performance and high availability, interested students can refer to my public number "Si Xuan architecture" Related Articles.

"How to improve system availability."

"Performance Optimization Starter: from idea to practice."

Guess you like

Origin www.cnblogs.com/shoshana-kong/p/11224209.html