Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

Seconds kill, just like the planned economy of the vegetable market, passers-by in a hurry, you come and I go. Bustling for a while, leaving only the lonely streets.

Take a look at the story of a goose. Left left left

Just yesterday. It was early morning, probably five or six o ‘clock, and a couple of programmers with shaggy hair were walking off work in a group. At this time, the street lamp has not put out simply. Several old men exercising, with their waists crossed, leaned forward and bent into a bow.

Therefore, a peddler selling geese at a fork in the alley is particularly conspicuous. He saw a flock of geese in a big cage with their wings fluttering. Some of the vigorous ones craned their necks and croaked, and the peddler, distracted by this, hit them on the head with a branch he was holding.

The peddler cocked his mouth and swept his mobile phone with the corner of his eye. It was already five sixty. At that moment, a crowd of old ladies gathered around, as if rising from the ground. At the beginning of the leisurely close, such as to see the cage as expected there is a flock of geese, holding the hand of the bag, scrambling to trot up.

The menacing movement startled everyone, and the programmers stood still. The old man’s waist is no longer bent, and even the noisy geese are no longer barking.

Soon an old woman helped the peddler to open the cage, without further ado, grabbed a goose by the neck and dragged it out. Then another hand grabbed another goose’s wing. All of a sudden people shouted goose hiss, noisy rang, under the goose feather snow. In a moment all the geese were in the hands of the aunts. But there were more old ladies who had not got the goose, wiping tears of remorse and jealousy with handkerchiefs.

Aunt Wang was the most delighted. She had a big hand and caught three of them. She held two of them in one hand and twisted their necks into a twist. There are also pinched dead geese, who do not want to commit the life of the goose, but there are more aunts do not mind.

The peddler heaved a sigh of relief that the trick was indeed effective, so he had a second kill. The geese suffering from disease were instantly disposed of at a low price.

It’s all in the eyes of the programmers. When the crowd was almost finished, they gathered the peddlers around, and one of them, with sharp eyes, shouted out, “Isn’t the goose man Xjjdog? “

“Xjjdog is me. We’d better find a churro restaurant, sit down and wait for me to give you, slowly.”

Seconds kill the difficulty

Second kill, so cool words, destined to be not a sheep, but the flood beast. Would you like to know the difference between 3W yuan and 3000W system?

Any situation that is not equal will cause a crisis, and the same is true for business systems. Second kill system difficult, mainly focused on the following points.

1. The traffic exceeds the carrying range



A sudden traffic

2. Resource conflict If traditional databases are used for data storage, serious lock conflicts will occur when the same resources are snatched. This is usually done through a front-facing, faster storage top, which involves data synchronization between the source and target libraries.

From the listing of commodity resources to the completion of the second kill, there will be a brief chaotic state, there will be inconsistent data. In the case of a very high concentration of requests, concurrency problems can also arise, and the behavior and results of individuals can be unpredictable.

3, the high difficulty of the infrastructure and technology requirements are also relatively high, from the access layer to the cache to the storage layer, as well as security considerations, require the participation of multiple components, and each component needs to be optimized.

All in all, there will be peace after all the noise. Hardware resources prepared for the second kill can not just sit there idle, so there is usually a resource release phase, this is a later story, we do not do too much attention.

Business Phase 3

Generally, the seckill service is divided into three phases. Among them, snap up stage, it is the second kill business that we often say.

1, preparation stage in the preparation stage, in addition to the preparation of hardware and software systems, there will generally be a warm-up of the activity, the Internet operation will be similar to the small advertisement on the telephone pole, widely spread. Once upon a time, a customer engaged in a second kill activity, 100 inventory 99 people participate, 99 people have 90 internal employees, embarrassed ha ha.

If you have your own APP, you can achieve better results through notification and subscription. If there are many commodities involved and the number of participants is huge, the data will be preprocessed and preheated in advance. When everything is ready, you can have a cigarette and wait for the countdown.

As the saying goes, one minute on the stage, ten years off the stage. When the seconds start, there will be a flood of instant requests, and it’s time to go on stage. At this stage, each of our systems and modules will quickly rotate, and any point that is not considered properly will result in the failure of the second kill activity, so the key components must ensure high availability.

As mentioned above, sectoids can have a transient chaotic state. In the liquidation phase, the final consistency of data needs to be completed, and the warehousing action may last for a long time. Some users may, at the moment of payment, regret, goods to return to the warehouse. Goods returned to the warehouse will be sold again, such as train tickets, 30 minutes later can be snatched again; Some items can be locked down and taken off the shelves forever.

Restriction principle

Here, we have a general overview of the seckill system in the technical aspects of the basic constraints principle, detailed description and code, we will be explained in the following chapter.

Data preheating

Some systems for second kill, may be so a few goods, manual entry are playing. For platform types, or apps with a large number of users, it’s not so easy to sell products in seconds.

In the middle, you do some merging of the data, or two-dimensional unwrapping, which is to prepare the data in seconds. After these data are processed, they will enter the second kill system for data preheating in advance.

Whether the second kill phase is successful depends on whether the data is prepared reasonably.

Request bearing

This is the outermost layer of the request and belongs to the access layer. A well-done system can block most requests at the access layer, greatly reducing the pressure on back-end services.

Connection number hosting The first challenge for the access layer is connection number. The general Internet access layer is LVS + NGINx. This will involve tuning the operating system, as well as tuning nginx itself.

Concurrent hosting Still has the problem of high concurrency for requests that land on a machine. Thread pool should be used reasonably, how to filter, merge and so on, there are certain challenges.

Load balancing requests must be properly balanced and do not cause hot issues. For example, Nginx’s IP_hash can avoid the distributed session problem to a certain extent, but the requests will be unbalanced.

Try again? Do not configure retry for the second kill service, which aggravates the system load. Request failed? Let’s do it again.

The system resources occupied by system isolation services are seriously inconsistent with those occupied by normal systems. If possible, the hardware and service environment of the seckill system should be isolated from the normal business system to some extent. Assess pressure on other services in advance to avoid disruption to normal business.

CDN takes up a lot of bandwidth for HTML, JS, CSS, images and other content. If all these resources are stored on the server, the bandwidth will be quickly occupied by traffic, and normal kill requests cannot be completed. CDN can effectively solve this problem. The rest of the request is the real kill business.

Reduction of network request packets requires a lot of optimization. You can use GZIP compression to compress resources, remove useless information in request packets, and simplify network packets.

Request to intercept

A very big principle of seckill systems is to block as many invalid requests externally as possible. Request interception can be divided into upstream interception and service interception.

Upstream interception For interception, there is a global setting. When the system is judged and reaches the bottleneck stage, the service can be degraded through full restricted flow mode, and for some secondary services, it needs to be fusing. For the front end, some optimization is also needed. Such as browser caching, anti-reentrant authentication, and so on, can block a significant number of requests.

Business interception, in addition to some global limitations, is relevant to the user for most requests. A user might refresh frequently, or bypass the front end and call the back end interface directly with the software. The illegal request of these users should also be intercepted.

At the same time, users should not wait indefinitely for resources. For example, for 100 goods in stock, when the 1w request comes, there is no need to wait in line any more.

For queueing, both within the JVM and an external message queue, MQ, is used to buffer requests.

Data cache

Caching is probably the most important component of a seckill system. From the front-end cache, to the JVM cache, to the distributed cache, system performance can increase by an order of magnitude. It is important to note that since seckill systems rely heavily on caching systems, caching systems need to be highly available.

The cache read operation takes into account the loading and synchronization of data. Write request, must consider the data merge and concurrent write, data consistency and so on. Although caching is much, much faster than DB, its performance is always a bottleneck and the relevant code needs to be optimized.

security

The barriers to entry are getting lower and lower. Second graders are teaching Swift, and writing a quick kill plugin is a breeze. The security of the second kill system is more important, it should be said that the system dealing with money is prone to problems. If you want to pull wool, look for marketing, second kill business, the right.

Try to raise the cheating threshold, such as URL dynamic, from the entrance to isolate most attacks; Captchas only increase the cost to the attacker. Some of the security level is higher, but also increase the risk control rules, such as the same IP request too much ban, account registration date within three days is not allowed to participate, the second kill threshold must be a gold member, etc..

I once had a script written for a captcha that used to add, subtract, multiply and divide. Five minutes before the second kill, xjjDog gave up the captcha 12306.

DB in the background

In the whole seckill system, the traditional DB can only hide in the background (except for small traffic). If I were DB, I’d be shivering in the back.

DB data, to be loaded in advance to the second kill system for calculation, second kill finished, but also the messy data for landing and liquidation. I’ve seen a lot of seconds kill scenes that don’t even require DB’s participation, it’s a brave artist.

This raises another question. Should the request penetrate if the cache system has problems? My advice is, no. Abnormal requests will crush DB instantly, resulting in more serious data confusion problems, if there is no isolation, the consequences will be more wild. Instead of being wrong, admit it and write a failure report.

End

Second kill, Raiders, P2P, is created by the Internet, the three big money killers.

I’m going to say something private. Not long ago, I had a good flock of geese, but it turned out to be sick after a few days with a chicken with a disease. It would be a pity to throw them away. The goose timeline, too, is tight.

Just yesterday, I put the word out that there was a shipment of cheap geese to be disposed of. In order to convince more damas, I took a 50% discount from the market price, even if I could sell it at a 10% discount (I dare not sell goose on Douyin). After 5:00, I injected a stimulant into some of the geese that were about to fail, hoping they’d last a little longer, and I knocked them over the head with a twig to make sure. Not that I’m confident, but in this scenario, you could sell a dead goose. But more dead, after all, is not good. In order to limit the crowd, I let out the wire corner of the cage. Many old ladies cut their hands and failed to catch the geese. I could not tell whether they were unlucky or lucky.

Who first created seckill? Genius. Like hunger marketing, it’s an IQ tax. Ha ha ha ~

Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.