Chapter 378: [Solutions and Quotations]

Latest URL: PM.

Luo Sheng is still staying at Bluestar Technology Group. He was going to host a technical meeting at the Cote d'Azur laboratory this afternoon, but after learning that the National Railway Group came to seek technical support and help, he temporarily modified his work arrangements. Choose to meet with Vice President Liu first.

The main reason is that the year is near, and people from all over the country, especially migrant workers who are migrant workers, are rushing to rush home to celebrate the New Year. This group is very difficult. Luo Sheng feels that he can do something for them, let alone help It's also a meaningful thing for them to easily buy a ticket to go home for Chinese New Year reunion.

Besides, it can also gain a big customer.

...

At this moment, in a company's reception room, Luo Sheng and Vice President Liu and other seven or eight national railway groups were together.

After a brief greeting, Vice President Liu found the entry point for key topics and said: "The development cost of the first phase of the 12306 ticketing system is 300 million yuan, including hardware. This system needs to be combined with the Ministry of Railways information database and also with the The combination of phone ticket sales, window ticket sales and other related data. 12306 is not a simple online transaction system, but the largest bulk cargo transportation system in China and the world. "

After a while, a accompanying technical expert also said, "The technology of 12306 is really very difficult to solve, and the existing distributed database, cache, and load balancing technologies can no longer meet the needs."

Speaking of which, the technical expert looked at Luo Sheng and added: "You need to know that 12306 has nearly 30 billion visits every day during the Spring Festival. Such high-level train ticket inquiry is an important factor affecting the performance of 12306. For one reason, high probability accounts for more than 90% of the visits. What's even more tricky is that the query of peaks and valleys is very different. There is almost no way to make a good balance between cost and concurrency. "

Listening Luo Sheng nodded silently. After the deceased had finished speaking, he asked: "How did your cooperation with Ali resolve?"

The technical expert said frankly: "The current approach is to control the traffic from several key entrances to ensure system availability, but it will affect the user experience, and the website is still down and cannot be opened. It has already been criticized."

Talking about the technical plane, he shook his head with a bitter smile.

Vice President Liu also said: "Ali provided technical support to 12306 for free, but the technology is still not hard enough, and we now put our hope on the nebula."

Luo Sheng couldn't help but smile, and said straightforwardly, "General Manager Liu, first of all, I can definitely give you a reply. The Nebula system is completely fine. At the same time, I can guarantee that a complete set of software can be created for 12306 within one week. To hardware complete solution, but this solution will not be supported for free like Ali. "

Mr. Liu said a dumb smile, saying, "I'd like to hear the details."

Luo Sheng thought for a moment, and quickly organized the language and said, "If Nebula supports the 12306 system, put the 12306 website query and access on the nebula. Generally speaking, the query is the most visited part of most ticketing systems. I just listened to you Experts said, about 90%. In the transaction-related process, query requests will be submitted multiple times, not to mention that now there are a lot of additional workload caused by the emergence of a lot of ticket-swiping software, all of which make the remaining votes The query system becomes a stress center for the entire system. "

"My solution is that after Nebulas intervened, the remaining ticket query module was separated from the existing system of 12306 and has the ability to be deployed independently; a set of remaining ticket query systems was independently deployed on the cloud, so that both 12306 and the cloud have A surplus ticket inquiry system will make scheduling more flexible. At present, the scale of Nebula cluster servers has reached more than 50,000. "

The technical experts of the railway group who were present also secretly pouted. It is no wonder that Ali's "flying sky" system can't help but beat the former, but the latter has reached a scale of 50,000, which is no wonder that Luo Sheng will have such a big confidence.

Next, Luo Sheng mainly discussed with several technical experts of the railway group.

"This morning, when I heard the news, I took a brief look at the server structure of '12306'." Luo Sheng said methodically to everyone: "The majority of visitors are spraying, but I know that once the 12306 service is put into trial operation, I will bear it. With a QPS that cannot be surpassed by any spike system in this world, millions of concurrency can't be more normal. "

A few of the key technical personnel of the railway group who were present were touched by their hearts, and lived to understand.

Leaders who do not understand technology have the most difficulty in communicating and feel that they have not done their best.

Luo Sheng's words are still very important.

After a while, Luo Sheng added: "The high-concurrency system architecture must be deployed in a distributed cluster. The upper layer of the service has layer-by-layer load balancing and provides various disaster recovery methods. The so-called disaster recovery methods are dual-fire engine room and node fault tolerance. , Server disaster recovery, etc. To ensure the high availability of the system, traffic will be balanced to different servers according to different load capacities and configuration policies. "

"Even so, the QPS that a single machine in the cluster can withstand is very high, so how to optimize the performance of the single machine to the extreme? To solve this problem, we must first understand one thing: usually the booking system has to deal with generating orders, reducing The three basic stages of deducting inventory and user payment are to ensure that train tickets are not oversold, many are sold, and every ticket sold must be paid to be effective, and the system must withstand extremely high Concurrency. "

Several railway group technical experts nodded again and again to agree, the technical pain point is here.

Luo Sheng continued: "Place orders and reduce inventory. When users make concurrent requests to the server, first create an order, then deduct inventory, and wait for users to pay. This sequence is the solution that most people would think of in the first place. In this case also It can guarantee that the order will not be oversold, but it will also cause some problems. The first is that in the case of extreme concurrency, the details of any memory operation will affect the performance, especially the logic such as order creation, which basically requires storage. When you go to a disk database, the pressure on the database is conceivable. 12306 should be an Oracle database. Don't spend this wrong money and put it on my nebula. "

"The second is that if the user has a malicious order, the inventory will be reduced if only the order is not paid, and a lot of orders will be sold. Although the server can limit the number of IP and user purchase orders, this is not true. A good way. "

"Then it is to pay less inventory. If you wait for the user to pay for the order and then reduce the inventory, the first feeling is that it will not sell less. But this is the taboo of the concurrent architecture, because in the case of extreme concurrency, users may create many orders When the inventory is reduced to zero, many users will find that the grab order cannot be paid, which is also called "oversold", and it is not possible to avoid concurrent operation of the database disk IO. "

"The last is withholding inventory. From the consideration of the above two scenarios, we can conclude that as long as the order is created, the database IO is frequently operated. So is there a solution that does not require direct operation of the database IO? The answer is yes Is to withhold inventory, deducting the inventory first to ensure that it is not oversold, and then generating user orders asynchronously, so that the response to the user will be much faster. "

"So how to guarantee a lot of sales? What if the user gets the order and does not pay? The order should be valid. For example, if the user does not pay within five minutes, the order will expire and new inventory will be added. The generation of the order is asynchronous , Should be processed in the instant consumption queue ... "

The vice president Liu listened to the fog, but he found that Luo Sheng and several technical experts he brought with him became more and more hot, and it seemed to have reached a message.

Find the right person!

At this time, Luo Sheng opened the large screen on the wall in the room. He also brought a laptop working computer to open it, signaled everyone to look at the main screen, and said while operating the computer himself:

"Go is natively designed for concurrency. I will use Go to show you the specific process of grabbing tickets on a single machine and my optimized solution."

"The init function in the Go package is executed before the main function. At this stage, some preparatory work is mainly done. The preparatory work that the system needs to do is: initialize the local inventory, initialize the remote redis storage unified hash key, and initialize redis. Link pool. "

"In addition, you need to initialize an int type chan with a size of 1. The purpose is to implement the function of distributed locks. You can also use read-write locks or use redis and other methods to avoid resource competition, but using channels is more efficient. This is the Go language. The philosophy is not to communicate through shared memory, but to share memory through communication. The Redis library uses redigo. Here is the code:

[

// localSpike package structure definition

packagelocalSpike

typeLocalSpikestruct {

LocalInStockint64

LocalSalesVolumeint64

}

...

// remoteSpike definition of hash structure and redis connection pool

packageremoteSpike

// Remote order storage value

typeRemoteSpikeKeysstruct {

...



...

Unconsciously ~ www.novelhall.com ~ time has passed an hour and a half, anyway, Vice Liu always can't understand, and those codes look dizzy.

Technical issues are finally discussed.

The technical experts of the Railway Group looked at Vice President Liu and nodded his head with a positive eye. Vice President Liu immediately understood, and decisively turned to Luo Sheng and asked: "General Luo, if you provide such a complete set Improve the solution, what is the quote? "

Luo Sheng bluntly said: "Pay 1 billion a year."

Vice President Liu heard that this number almost did not spit out old blood, and the budget for the first phase of the 12,306 ticketing system was only 300 million. Such a quotation is simply too expensive to accept.

...

()

Chinese website