Star profile

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

Time: New Era 2036.

About the planet:

  • Chinese name: A-731 e-commerce planet

  • A-731 Mall

  • Category: Planet

  • Revolution period: one year

  • Resident User:

    • Various requests
      • Order request
      • Retreat a single request
      • Shipping requests and so on
    • Urban worker
      • Nginx workers
      • CDN workers
      • Redis workers and so on
  • Total planet history: 200,000 years.

Planet crisis

I am a snapkill request, and my daily job is to ship the snapkill request data to the backend workers.

One day, I met the request small kong on the Nginx forwarding server. I told him that it was not convenient to tell him the important news now, and I would make an appointment after work. Then we hurried on, because we both had to quickly send the request data to the order planet.

Kong and I went to a bar after work at 10 p.m., ordered two mojito and found a corner to sit down.

Kong: You seem preoccupied lately.

Me: Have you noticed that recently the order number of our planet has increased dramatically, 10 million order data are generated every day, it is not a day, two days.

Xiao Kong: No wonder I work until ten o ‘clock every night to deliver the requested data.

Me: I have an uncle who works in the space agency and told me that our planet can’t handle so many requests and order data that there will be a planetary explosion soon.

Kong: What should I do then?

Me: We can go to T-714, which is 100 light years away, but we can only get to that planet on a time shuttle through the Seckill tunnel. And there’s a quota, so I don’t know if I’ll get on the shuttle.

Me: The tunnel will be open twice tomorrow, 10 a.m. and 2 p.m. Why don’t you come with me tomorrow?

Kong: Ok.

Knowledge points involved:

  • What do we mean by a big planetary explosion?
    • Because of the large amount of order data, the database can not support. The database may be down.
    • The server is overwhelmed by the volume of requests being sent to it every day. The server may be down.
  • What does it mean that the seckill channel is open twice a day?
    • Staggered peak flow, split the flow to two seconds kill times.
    • Of course, the means of wrong peak flow and input verification, adding to the shopping cart and other flow sharing operations.

Down the channel

Location: A-731 Planet Airport

Time: 9:45

“Request passengers bound for T-714 will line up on platform Y1 for special access to the shuttle concourse in 15 minutes.” The hall announcement was played three times in a row.

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

Knowledge points involved:

  • Why does the seckill scene have a separate channel?
    • Second kill service A second kill system is deployed independently to avoid affecting other services in the system.
    • Summed up as service single responsibility + independent deployment

Real-time screen

I looked up and saw a large screen at the top of the channel, constantly showing photos of T-714 and ticket order information.

Two uniformed workers were patrolling beside the screen. Nginx on one uniform, CDN on the other.

Knowledge points involved:

  • Nginx uniforms:

    • Nginx uniformed workers maintain Nginx’s static and dynamic resources.
    • The product details page is a static page that is stored on an Nginx server. When accessing a static resource, the request goes to Nginx first, and then the Nginx server matches the requested URL link to whether the static resource is accessed.
    • The large item details page is not retrieved from the backend server by sending a request. In fact, the separation of dynamic and static is achieved.
  • A diagram explaining Nginx static separation

    • Static resources such as HTML files with minimal changes can be placed on a dedicated server and accessed directly without interaction with a backend server (such as Tomcat).
    • Dynamic resources, for example, need to know how many people have bought goods from the background and send an order request to store data. These are called dynamic resources, which cannot be narrowly understood as visible resources. In a broad sense, they can include obtaining the results of logical processing and performing operations such as storing data.
  • CDN uniform

    • What is CDN? CDN is a kind of service that allows users to obtain resources nearby, which reduces network transmission time and improves access speed.
    • Nginx hosts HTML files, while CDN hosts images and scripts introduced by HTML.
    • CDN uniformed workers are maintaining CDN resources.
  • A flow chart explains how CDN works

Validation of the channel

Time: 11 a.m.

“Authentication channel has been opened, please enter with your password!” Three more broadcasts were made.

Knowledge points involved:

  • Why do I need a password?
    • To prevent a large number of simulated kill requests from entering the business process, a validation is first added to discard these bogus requests.
    • How did you do that? The front-end web page first sends a request to get the password, and when you click to buy it, the request body carries the encryption password, and the back-end verifies whether the password matches. You can use MD5 encryption.
  • Summary: seckill request encryption.

Shuttle hall

After screening through the verification channel, half of the fake requests were blocked, and those like me with the correct password made it into the shuttle lobby.

Arriving at the hall, I found a monitor placed in the center of the hall with the red number 100 on it.

On the left-hand side of the screen stands a pretty woman in a Redis uniform. As I watched, I overheard her controlling the monitor to show the remaining number of the shuttle. If the number goes to zero, the shuttle is fully occupied, and the subsequent crew will have to return in vain.

Knowledge points involved:

  • In the second kill scenario, the remaining inventory is not queried directly in the database, but in the Redis cache.
  • Why cache? Because looking up the cache is much faster than looking up the database, the response time is reduced and the strain on the database is much reduced. If too many requests to check the inventory are sent to the database, the database will crash and the database will not be able to do any other work.

Rob tickets

To the right of the screen stood a handsome young man in a sharp suit, with a red armband on his sleeve bearing the words Redisson. With a solemn face, he ignored the clamor of requests in the hall. Probably used to seeing this all the time.

Is looking at the handsome boy, found his left hand holding a stack of air tickets, yes, with a ticket can log into the shuttle. I arrived in front of him at the speed of one hundred meters, arrived in front of him, there have been more than a dozen requests to his side, he in accordance with the first come, first come order issued tickets, to my time, there are only a few tickets, fortunately, my one hundred meters rushed to help 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 woman in a Redis uniform operates the screen to reduce the number by one.

Ten seconds later, when the tickets had been issued, the display showed the number 0.

Knowledge points involved:

  • What is a Redisson? Redis client, solves some common problems of distributed.
  • Redisson’s semaphore function is used here, so there are 100 tickets, so there are 100 semaphore tickets, and the number of tickets is not oversold due to multi-threaded concurrency or distributed systems. Like 101 tickets sold.
  • Each person can only get one ticket, which is the idempotence check involved in the seckill system.

A boarding pass

The handsome guy who issued the ticket told me that after I got the ticket, I had to wait in line at window A to pay and then I could get the registration card. So I was in line at window A with 99 other requests.

When I saw a request, I wanted to give up the payment, saying that the air ticket was too expensive. When I was about to leave the hall, I was stopped by the handsome guy who issued the air ticket. He asked if the request should be considered, and there was 15 minutes for consideration.

Knowledge points involved:

  • Queue peak-cutting commonly used in seckill systems. The successful request is queued, and the order is slowly created and the inventory is reduced.

  • After the second kill is successful, the user is quickly told that the second kill is successful, instead of telling the user after waiting for the order to finish, the user will have to wait for a long time, affecting the experience.

  • Why do queue peaking? Successful requests don’t have to go to the database to create orders all at once, which puts less strain on the database.

  • In the second kill scenario, it is very likely that some users grab the scene but do not pay, at this time the inventory should be added back, can be provided to other users.

Does the ship leave

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

Let’s go. To T-714. I’ve heard that the planet’s databases are being split and tabulated, and services are being split into microservices.

conclusion

The above mentioned points of concern in seckill system are explained in the way of science fiction, but they are not covered completely, so here are eight points of concern in seckill system:

  • The service has single responsibility and independent deployment
  • Inventory warm-up, rapid deduction
  • Seckill link encryption
  • Dynamic and static separation
  • Malicious request interception
  • Traffic peak
  • Current limiting & fusing & Downgrade
  • Queue peak clipping

Only speak principle seems to be not interesting, is it to a real combat!

Hello, I’m Brother Wukong, “7 years of project development experience, full stack engineer, development team leader, love the basic principles of graphic programming”.

I also handwritten 2 small program, Java brush small program, PMP brush small program, click on my public menu to open! There are also 111 architect profiles and 1,000 Java interview questions, all in PDF format. You can follow the public account “Wukong chat framework” to reply to Wukong to receive quality information.

“Forward -> see -> like -> favorites -> comment!!” Is the biggest support for me!