Chapter 378 [Solution & Quotation]
Afternoon.
Luo Sheng is still staying in the Bluestar Technology Group, and he was going to the Cote d'Azur Laboratory to host a technical meeting this afternoon, but after learning that the National Railway Group came to seek technical support help, he temporarily modified his work arrangement and chose to give priority to meeting Vice President Liu.
The main reason is that the New Year is approaching, people all over the country, especially migrant workers who go out to work, are in a hurry to rush home for the New Year, this group is not easy, Luo Sheng feels that he can do something for them, not to mention helping them buy a ticket home for the New Year reunion more easily is also a very meaningful thing.
Moreover, it can also gain a big customer.
……
At this moment, in a reception room of the company, Luo Sheng and Vice President Liu and seven or eight other people from the National Railway Group were together.
After a brief meeting and a few words of greeting, Vice President Liu found the entry point of the key topic and said: "The development cost of the first phase of the 12306 ticketing system project is 300 million, including hardware, and this system needs to be combined with the information database of the Ministry of Railways, and also needs to be combined with the combination of telephone ticketing, window ticketing and other related data. 12306 is not a simple online trading system, but the largest bulk material freight system in the country and even the world. ”
After a while, a technical expert accompanying him also said: "The technology of 12306 is indeed very difficult to solve, and the use of existing distributed database, cache, and load balancing technologies can no longer meet the needs. ”
Speaking of this, the technical expert looked at Luo Sheng and added: "You must know that 12306 has an average of nearly 30 billion visits per day during the Spring Festival. Such a sky-high level of train ticket query is one of the important reasons affecting the performance of 12306, accounting for more than 90% of the visits. Even more tricky: peaks and valleys are so different that there's almost no way to strike a good balance between cost and concurrency. ”
Luo Sheng, who was listening, nodded silently, and after the deceased finished speaking, he asked, "How did you solve the cooperation with Ali?" ”
The technical expert said frankly: "The current approach is to control the flow from several key entrances to ensure the availability of the system, but it will affect the user experience, and there is still a phenomenon that the website is down and cannot be opened, which has been scolded." ”
Saying that, the technical plane also shook his head with a wry smile.
Vice President Liu also said: "Ali provides technical support to 12306 for free, but the technology is still not hard enough, and we are now putting our hope on Nebula." ”
Luo Sheng couldn't help but smile, and said straightforwardly: "Mr. Liu, first of all, I can give you a reply for sure, the Nebula system is completely fine, and I can guarantee that a complete set of software and hardware solutions will be tailored for 12306 within a week, but this solution will not be supported for free like Ali." ”
Mr. Liu was dumbfounded, but also smiled, and said, "I would like to hear it in detail." ”
Luo Sheng thought for a moment, quickly organized the language and said: "If Nebula supports the 12306 system, put the 12306 website query and access on Nebula. Generally speaking, inquiries are the most visited part of most ticketing systems, and I just heard from your experts, about 90%. In the process of transaction, multiple query requests will be submitted, not to mention the additional workload brought about by the emergence of a large number of ticket swiping software, all of which make the remaining ticket query system a pressure concentration for the entire system. ”
"My solution is that after Nebula intervenes, the remaining ticket query module will be separated from the existing system of 12306, and it will have the ability to deploy independently; Independently deploy a set of remaining ticket query system on the cloud, so that 12306 and the cloud have a set of remaining ticket query system, and the scheduling will be more flexible, and the current scale of Nebulas cluster servers has reached more than 50,000. ”
Several technical experts of the railway group present also secretly smacked their lips when they heard it, no wonder Ali's "Feitian" system was so can't help fighting, the former barely broke a thousand, and the latter has reached a huge scale of 50,000, no wonder Luo Sheng is so confident.
Next, Luo Sheng mainly discussed with several technical experts from the railway group.
"This morning, when I learned the news, I briefly studied the server architecture of '12306'." Luo Sheng said methodically to everyone: "The majority of visitors are spraying, but I know that as soon as the 12306 service is put into trial operation, it will suffer from QPS that cannot be surpassed by any seckill system in the world, and millions of concurrency is normal." ”
Several of the technical backbone personnel of the railway group present were moved in their hearts, and long live their understanding.
Leaders who don't understand technology are the most difficult to communicate and feel that they are not doing their best.
Luo Sheng's words still carry a lot of weight.
After a while, Luo Sheng added: "The high-concurrency system architecture should be deployed in distributed clusters, with layers of load balancing at the upper layer of the service, and various disaster recovery methods are provided, the so-called disaster recovery methods are double fire machine room, node fault tolerance, server disaster recovery, etc. To ensure the high availability of the system, the 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 a single machine to the extreme? To solve this problem, we must first understand one thing: usually the booking system has to deal with the three basic stages of generating orders, deducting inventory, and user payment, and what the system has to do is to ensure that the train tickets are not oversold, not a lot of sales, and each ticket sold must be paid to be effective, and to ensure that the system bears extremely high concurrency. ”
Several technical experts from the railway group nodded in agreement, and the technical pain point is here.
Luo Sheng continued: "Place an order to reduce inventory. When a user's concurrent request arrives at the server, the order is first created, and then the inventory is deducted and the user is waiting for payment. This order is the first solution that we ordinary people will think of, in this case, it can also ensure that the order will not be oversold, but there will also be some problems, the first is in the case of extreme concurrency, the details of any memory operation are crucial to affect the performance, especially the logic like creating an order, which basically needs to be stored in the disk database, the pressure on the database is conceivable, 12306 should be used in the oracle bone database, don't spend this unjust money, put it on my nebula. ”
"The second is that if the user places an order maliciously, only the order is not paid, so the inventory will be less, and there will be a lot of less orders, although the server can limit the number of IP and user purchase orders, but this is really not a good way."
"Then it's time to pay for the reduced inventory. If you wait for the user to pay the order and then reduce the inventory, the first feeling is that you will not sell less. However, this is a big taboo of the concurrent architecture, because in the case of extreme concurrency, users may create a lot of orders, and when the inventory is reduced to zero, many users will find that the orders they grabbed cannot be paid, which is the so-called 'overselling', and it cannot avoid concurrent operations on database disk I/O. ”
"Finally, there's the withholding inventory. From the consideration of the above two scenarios, it can be concluded that as long as an order is created, the database IO must be frequently operated. So is there a solution that doesn't require direct manipulation of database IO? The answer is yes, that is, the inventory is withheld, the inventory is deducted first, and the user order is not oversold is guaranteed to be asynchronously, so that the response to the user will be much faster. ”
"So how do you guarantee a lot of sales? What should I do if the user gets the order and doesn't pay? Orders should be valid, for example, if the user does not pay within five minutes, the order will become invalid and new inventory will be added. The generation of orders is asynchronous and should be placed in the instant consumption queue for processing......"
Vice President Liu listened to it in a fog, but he found that Luo Sheng and several technical experts he brought were communicating more and more hotly, and he seemed to have come up with a message.
You've found the right person!
At this time, Luo Sheng turned on the big screen on the wall in the room, and he also brought a notebook work computer to open it, motioning everyone to look at the main projection screen, and said while operating the computer:
"Go is natively designed for concurrency, so I will use Go to show you the specific process of stand-alone ticket grabbing and my optimized solution."
"The init function in the Go package is executed before the main function, and some preparatory work is mainly done at this stage. The system needs to initialize the local inventory, initialize the hash key of the remote Redis storage unified inventory, and initialize the Redis link pool. ”
"In addition, it is necessary to initialize a size 1 int type chan, the purpose is to implement the function of distributed locks, you can also directly use read-write locks or use redis and other ways to avoid resource competition, but it is more efficient to use Channel, this is the philosophy of Go, not to communicate through shared memory, but to share memory through communication. The Redis library uses redigo, and here's the code:
【
localSpike package struct definition
package localSpike
type LocalSpike struct {
LocalInStock int64
LocalSalesVolume int64
}
…
remoteSpike's definition of the hash structure and redis connection pool
package remoteSpike
Remote order storage of healthy values
type RemoteSpikeKeys struct {
……
】
……
Unconsciously, an hour and a half has passed, and Deputy Liu always doesn't understand it anyway, and the code is dizzy.
The technical issues are finally almost discussed.
The technical expert of the railway group looked at Vice President Liu and nodded his head with affirmative eyes, Vice President Liu immediately understood, and decisively turned to Luo Sheng and asked: "Mr. Luo, if you provide such a complete set of solutions, what is the quotation?" ”
Luo Sheng said bluntly: "Pay 1000000000000000000000000000000 ”
Vice President Liu almost didn't spurt out a mouthful of old blood when he heard this number, and the budget for the first phase of the 12306 ticketing system project was only 300 million, which was so expensive that it was unacceptable.
……
()
Chinese Net