I am a request for a spike, and I am escaping from this planet...

Author | Wukong chat framework

Source | Wukong Chat Architecture (ID: PassJava666)


Introduction to Planet

Location: β-410 galaxy, A-731 e-commerce planet.

Time: New Era 2036.

About the planet:

  • Chinese name: A-731 E-commerce Planet

  • Foreign name: A-731 Mall

  • Category: Planet

  • Revolution period: one year

  • Resident users: middleware workers, various requests.

  • The total history of the planet: 200,000 years.

Planetary crisis

I am a spike request, and my daily job is to ship the spike request data to the back-end workers.

This day I met the request "Xiaokong" on the Nginx forwarding server. I told Xiaokong that there was an important news that was not convenient. Now I told him to make an appointment after get off work, and then I hurried on the road.

Xiaokong and I went to a bar after get off work at ten in the evening, ordered two glasses of mojito, and sat down in a corner.

Xiao Kong: You seem to be worried recently.

Me: Have you noticed that the number of orders on our planet has increased sharply recently, and the data of 10 million orders generated every day is not a matter of one or two days.

Xiaokong: No wonder I work overtime every day until ten in the evening to deliver the requested data.

Me: I have an uncle who works for the space agency and told me that our planet cannot carry so many requests and order data, and there will be a "planetary explosion" soon. Don't reveal it to others.

Xiao Kong: What should I do?

Me: We can go to planet T-714 100 light-years away, but we can only go to that planet by using the time shuttle through the spike channel. And there are restrictions on the number of places. I don’t know if I have a chance to board the shuttle.

Me: The tunnel will be opened twice tomorrow, at 10 am and 2 pm. You go with me tomorrow!

Xiao Kong: Okay.

"The knowledge points involved:"

  • What is meant by the planetary explosion here?

    • Due to the large amount of order data, the database could not support it. The database may be down.

    • Because a large number of requests are sent to the server every day, the server can't handle it. The server may be down.

  • What does the spike channel open twice a day mean?

    • "Traffic peak shift", the traffic is divided into two spikes.

    • Of course, "traffic peak shift" methods include input verification, adding to shopping carts, etc. to share traffic.

Spike channel

Location: A-731 Planet Airport

Time: 09:45

aisle

"Please go to the planet T-714 to request passengers to wait in line at platform Y1 to enter the special passage, and enter the shuttle hall in 15 minutes." The broadcast in the lobby was broadcast three times in a row.

I walked to the special channel and saw a sign standing next to the channel: the spike channel, only for spike requests.

"Involving knowledge points:"

  • Why did the spike scene create a separate channel?

    • In order not to affect other services of the system, the spike business has deployed a set of spike system separately.

    • Summarized as "Service Single Responsibility + Independent Deployment"


Real-time big screen

As soon as I looked up, I saw a large screen above the passage, constantly playing photos of the planet T-714, as well as ticket order information.

Two uniformed workers are patrolling by the big screen. Nginx is printed on one uniform, and CDN is printed on one uniform.

Nginx+CDN

"Involving knowledge points:"

  • Nginx uniform:

    • Workers in Nginx uniforms are maintaining Nginx's static and dynamic resources.

    • The product detail page is a static page. These static pages are stored on the Nginx server. When accessing static resources, the request first goes to Nginx, and then the Nginx server matches whether it is the accessed static resource through the requested URL link.

    • The large-screen product detail page is not obtained from the background server by sending a request. In fact, "movement and static separation" has been realized.

  • A picture explains Nginx dynamic and static separation

    Nginx flowchart

    • Static resources such as HTML files rarely change, they can be placed on a server for direct access, without interacting with the backend server (such as Tomcat).

    • Dynamic resources, such as the need to get from the background how many people have purchased a product, send an order request to store data, these are called dynamic resources, can not be narrowly understood as visible resources, broadly can include the results of logical processing , Perform operations such as storing data.

  • CDN uniform

    • What is CDN: The vernacular explanation of CDN is that users obtain resources nearby, reduce network transmission time, and increase access speed.

    • HTML files are placed on Nginx, and image files and script files imported by HTML are placed on CDN.

    • Workers in CDN uniforms are maintaining CDN resources.

  • A flowchart explains the working principle of CDN

    CDN flow chart


Verification channel

Time: 10:00

"The verification channel has been opened, please bring the password to enter!" The broadcast was broadcast three times.

"The knowledge points involved:"

  • Why do I need a password?

    • In order to prevent a large number of simulated spike requests from entering the business processing flow, a verification is added first, and these fake requests are discarded.

    • How did you do it? The front-end web page first sends a request to get the password. When you click to buy, the encrypted password is carried in the request body, and the back-end verifies whether the password matches. It can be encrypted by MD5.

  • Summarized as "seckill request encryption".


Shuttle hall


After screening by the verification channel, half of the fake requests were blocked outside the door, and those like me who got the correct password entered the shuttle hall smoothly.

When I came to the hall, I found a monitor placed in the center of the hall, and the red number 100 displayed on it came into my eyes.

On the left hand side of the display stands a beautiful girl in a Redis uniform. I overheard that she was controlling the monitor to display the remaining number of the shuttle. If the number becomes 0, it means that all the shuttles have been occupied, and the people who follow will have to return without success.

Redis

"The knowledge points involved:"

  • In the spike scenario, querying the remaining inventory is not directly checking the database, but checking the Redis cache.

  • Why check the cache? Because the speed of checking the cache is much faster than checking the database, the response time is reduced, and the pressure on the database is reduced a lot. If many requests for inventory checking are made to the database, the database will collapse, and the database cannot do other tasks.


Grab votes

On the right hand side of the display stood a handsome young man in a straight suit, and saw a red armband hung on his cuff with the words Redisson printed. With a serious expression on his face, he ignored the dark requests in the hall. Maybe you are used to this kind of scene.

When I was looking at this handsome guy, I found that he was holding a stack of tickets in his left hand. That's right, you can log in to the shuttle with a ticket. I arrived in front of him at a speed of 100 meters. When I arrived in front of him, a dozen requests had already come to him. He issued air tickets in order of first-come-first-arrival. By my time, there were only a few tickets left. Fortunately, my 100-meter rush speed helped me grab a ticket. I asked the handsome guy if he could send me another ticket, but he refused.

Every time a ticket is issued, a pretty girl in a Redis uniform will operate the display to reduce its number by one.

Ten seconds later, the ticket has been issued and the display shows the number 0.

"The knowledge points involved:"

  • What is Redisson? The Redis client solves some common problems of distributed.

  • Here it is practical to the Redisson semaphore function, there are a total of 100 tickets, that is, 100 semaphores, and the number of tickets will not be oversold due to multi-threaded concurrency or distributed systems. For example, 101 tickets were sold.

  • Each person can only get one ticket. This is the idempotence check involved in the spike system, and you cannot repeatedly grab tickets.

boarding pass


boarding pass

The handsome guy who issued the ticket told me that after getting the ticket, he must queue up at window A to pay before he can get the registration card. So I queued up in Window A with 99 other requests.

Seeing a request that I wanted to give up the payment, saying that the ticket was too expensive, and when he was about to leave the lobby, he was stopped by the handsome guy who issued the ticket. He asked if the request should be considered. There was 15 minutes to consider. If the request still felt No, you can return the ticket to him, and he can issue it to others.

Queue clipping

"The knowledge points involved:"

  • "Queue peak clipping" commonly used in spike systems. Spike successful requests enter the queue, slowly create orders and deduct inventory.

  • After the spike is successful, quickly tell the user that the spike has been successful, instead of waiting for the order to finish and then tell the user, then the user will have to wait a while, which will affect the experience.

  • Why do you want to do queue peak clipping? Successful requests do not have to go to the database to create orders at once, so the pressure on the database will be less.

  • In the spike scenario, there may be scenarios where the user grabs it but does not pay. At this time, the inventory is added back and can be provided to other users.


set sail

After the order was successfully created, I successfully got the boarding pass, and after passing the verification of the boarding pass, I successfully boarded the shuttle.

Set off, go to planet T-714. I heard that the database of that planet has been divided into databases and tables, and services have also been split into microservices.


to sum up

The above explained the points of concern in the spike system through science fiction, and the following is a summary of the eight points of concern in the spike system:

Focus on the spike scene

  • Service single responsibility, independent deployment

  • Inventory preheating, fast deduction

  • Spike link encryption

  • Dynamic and static separation

  • Malicious request interception

  • Flow peak

  • Current limit & fusing & downgrade

  • Queue clipping

It doesn't seem like just talking about the principle, is it going to be a real station?


更多精彩推荐
☞多样性计算时代,怎样的技术生态才能满足发展需求
☞牛!发出中国第一封电子邮件,注册登记域名CN,中国互联网之父传奇
☞苹果回应iPhone12用5G耗电快;央行:微信支付宝和数字人民币不存在竞争关系;Win10X 将于年底签署 RTM|极客头条
☞算力至上?四大AI芯片大对决
☞大数据给教育带来怎样的可能?
☞干货 | 以太坊上的数字签名
点分享点点赞点在看

Guess you like

Origin blog.csdn.net/csdnsevenn/article/details/109301906