For some think high concurrency

When thinking of supper

Troubleshooting just for an hour, and finally get, reward yourself a bowl of instant noodles, such as a microwave oven before when waiting like a lot of things.

Concurrent seems to be a nightmare for all programmers, but for all programmers of "food", if the computer does not support concurrent, Error Log in the world should be able to less than half, while the programmer's "magic" is also one-third less.

So-called spike system is a kind of refers to, on behalf of multiple concurrent, highly available system. Why has not the same as a similar Spring framework can stand it in the concurrent field, because each of these areas, the situation is not the same as each scene, each solution concurrent program, divorced from reality, there is no value.

Familiar, I'm still thinking, if possible, I will how to achieve a high concurrent program yet.

In my recent book, "spike" system, for example, a group of people, at the same time, grab a thing. I can how to optimize it?

Starting from the database: reduce database operations

Database where? Disk. CPU speed and disk speed difference between the times? Ma's almost like the speed to make money and my money rate gap now. Therefore, we must reduce the access to the database, as little less.

There is another reason, if each one more operation of the database, the highly concurrent risk due to inconsistent data is also high a point. (Dirty reads, phantom read, etc.) How to solve this problem? Cache chant, chant lock. This is nothing new, thinking about the cache, the back talk anymore.

Database also divided: the order DB, paid DB. Try not to arrange a plurality of objects on a table DB.

In addition, there is a probability, my database class when a program thought:

You can set up two machines, the above database is the same (two-point), one responsible for reading, one responsible for writing? This will not only solve the backup problem, we have solved the problem of switching IO.

Starting from the user: increased answer session

This solution is a little wicked, but also essential, in addition to intercept scalping robot, but also the user surge diversion, just as dams, flood suddenly stopped, then slowly put out. However, the subject can not be set too remote (e.g., early 12306).

When I write code "spike" feature today, each SQL statement is written, I think, this necessary?

Really is not necessary! Reproduce it, when I finished writing "buy" feature, the normal idea is what? Check inventory? This database once operated. High point of the practice is to "inventory data" in the cache among the queries every purchase.

Another high point of the practice is to take preventive measures, when I was in the user "diversion" I just put the stock equal to the number of requests, and then close the interface. The people behind do not have to query the database and cache, and you come late.

Starting from the system: System Classification

Here the system refers to a system based on functional division, in the "spike" system, the highest level what system? Payment system, then the coupon system. We can now pay level setting function of the system is the highest, when resources are not enough, given the high priority level assigned by the system. Do not let unimportant system slow down the high-order system, this approach also brings evils "and talked about" two-eleven is the last time, you can not change the shipping address. (Refund system can not run)

Also, do not let the high-order system to rely on low-level system, so self-contradictory.

Description This practice is already a standard in the industry, in fact, address the high concurrency, is a choice.

Data from the start: the layout in advance

Or double eleven example: you are not a month will start looking for your prey before the two-eleven?

Then add to cart. By the way, similar to a shopping cart through the system, the background to know what something is "hot data", and then they were "optimized" and "isolation."

Optimization can understand, but what does this mean isolation?

We can not request to bring a small portion of the goods and caused the collapse of most commodities, it is best to isolate hot commodity, beautiful women who grab half of your cosmetics, I do not bother to grab a coffee break.

There are coupons, etc. These are actually can be calculated in advance, no need to waste time concurrent computing resources, most people are finished in a shopping cart to calculate the final price, then click to buy Qiazhao time.

Therefore, the coupon system in the two-eleven, and did not cause much of a performance penalty. Instead, it becomes a math problem.

Starting from the cache: clustered and divided

In the world of high concurrency, the cache is money. Cluster is the army.

Important data such as inventory, totally worth carved out a cache cluster exclusively for his use.

Like big brothers payment systems, also well worth dividing a cluster to him.

Cache cluster What are the benefits? Easy to maintain, good care, good customization. According to the characteristics of data to be processed, to customize and maintain, such as processing cache inventory data, we can not support the full range of search. Through a unique ID commodities were HASH, located directly on the barrel.

No matter how high-level point on the "Cache" in hash. To each machine had a bucket, and then build a large hashMap. Retrieval speed HashMap all to see.

Start from the queue: Queue Service

Service here, referring to the inventory service, order this service, we can be queued at the application layer, to jump the queue, such as inventory service priorities.

Temporarily write this now, a little late.

Guess you like

Origin www.cnblogs.com/Jun10ng/p/12393180.html