preface

Recently, I have been pushed a lot of articles on secakill architecture. I have also summarized the architecture design of Internet platform secakill in my spare time, and of course I have learned from many students’ ideas. As the saying goes, apart from the case architecture is playing rogue, the final use of SpringBoot simulation to achieve part of the second kill scene, at the same time to share with you.

Seconds kill scenes

Split-kill scenarios are nothing more than multiple users snapping up one or more items at the same time, and the term is known as high concurrency. In reality, we often like to see the scene, a group of aunts buying discounted eggs picture must be familiar, such a scene let the waiter elder sister very helpless, catch up with no money.

Business characteristics

  • Instant high concurrency, computer next to the little brother, young lady sisters such as the supermarket to rob the aunt general, crazy point the mouse
  • Stock is small, cheap, scarce and limited, worth snapping up, such as apple kidney, millet flour, hammer powder (long live understanding)

The user scale

User size can be large or small, hundreds or thousands of people active single architecture is enough to cope with, simple locking, in-process queuing can be easily done. Once you scale up to millions and tens of millions, consider distributed clusters to handle instantaneous high concurrency.

Seconds kill architecture

Architecture level

  • The general business when doing activities, often encounter a variety of malicious DDOS attacks (using innocent people to seize resources), resulting in the real we can not get services! Therefore, high IP protection is necessary.
  • Activity means that there are more people. SLB is connected to distribute traffic to multiple cloud servers, which can expand the external service capability of application systems through traffic distribution and improve the availability of application systems by eliminating single points of failure.
  • Considering the price and flexibility of SLB, we will access Nginx for stream limiting distribution to ensure the normal operation of back-end services.
  • Back-end seckill business logic, based on Redis or Zookeeper distributed lock, Kafka or Redis to do the message queue, DRDS database middleware to achieve data read and write separation.

Optimization idea

  • Shunt, shunt, shunt, important things to say three times, even the most awesome machine can not withstand a high level of concurrency.
  • Limit the flow, limit the flow, limit the flow, after all, the second kill goods are limited, there is no absolute fairness under the premise of brush prevention, according to the load capacity of each service, set the flow limit.
  • Cache, cache, cache, try not to let a large number of requests through the DB layer, the commodity information can be pushed to the distributed cache before the activity starts.
  • Asynchronous, asynchronous, asynchronous, analyzing and identifying logic that can be processed asynchronously, such as logs, to reduce system response time.
  • Active/Standby mode, active/standby mode, and active/standby mode are also required. (For details, see Hammer Is Attacked in a Year.)
  • Finally, in order to support higher concurrency and pursue better performance, the deployment model of the server can be optimized. Some requests go through the normal seconds kill process, and some requests directly return seconds kill failure. The disadvantage is that two sets of logic need to be maintained during development and deployment.

Hierarchical optimization

  • Front-end optimization: generate static commodity page push cache and CDN before the activity starts, and static file (JS/CSS) request push to file server and CDN.
  • Network optimization: If it is a national user, it is best to use BGP multi-line machine room to reduce network delay.
  • Application service optimization: Nginx optimal configuration, Tomcat connection pool optimization, database configuration optimization, database connection pool optimization.

Full-link pressure measurement

  • Analyze the system involved in the piezometric service scenario
  • Coordinate all pressure system resources and set up pressure environment
  • Pressure measurement data isolation and monitoring (response time, throughput, error rate, and so on are displayed in real-time as charts)
  • Pressure test results statistics (average response time, average throughput and other data will be displayed in the form of charts after the test)
  • Optimize individual system performance, associated processes, and entire business processes

The whole process of pressure measurement optimization is a process of continuous optimization and improvement. In order to improve the stability and performance of the system, problems can be found through testing in advance, optimize the system, avoid problems and specify emergency plans.

Code case

Maybe we all understand the principle of seckilling architecture, and there are many implementation methods on the Internet, but most of them are written descriptions, telling you how to lock, cache, queue and so on. But there are few comprehensive cases that tell you how to do it, and since you’re going from 0 to 1, hopefully the code examples below will help you. Of course, the final implementation of production, there is still a long way to go, according to their own business coding, implementation and deployment.

You will learn the following from the code case (supplemented from time to time) :

  • How do you SpringBoot microservices
  • The use of ThreadPoolExecutor thread pools
  • Scenarios of ReentrantLock and Synchronized
  • Database locking mechanisms (pessimistic locking, optimistic locking)
  • Distributed lock (RedissLock, Zookeeper)
  • In-process message queues (LinkedBlockingQueue, ArrayBlockingQueue, ConcurrentLinkedQueue)
  • Distributed message queues (Redis, Kafka)

Code structure:

├ ─ SRC │ ├ ─ the main │ │ ├ ─ Java │ │ │ └ ─ com │ │ │ └ ─ itstyle │ │ │ └ ─ seckill │ │ │ │ Application. Java │ │ │ │ │ │ │ ├ ─ common │ │ │ │ ├ ─ API │ │ │ │ │ SwaggerConfig. Java │ │ │ │ │ │ │ │ │ ├ ─ config │ │ │ │ │ IndexController. Java │ │ │ │ │ │ │ │ │ ├ ─ dynamicquery │ │ │ │ │ dynamicquery. Java │ │ │ │ │ DynamicQueryImpl. Java │ │ │ │ │ NativeQueryResultEntity. Java │ │ │ │ │ │ │ │ │ ├ ─ the entity │ │ │ │ │ Result. Java │ │ │ │ │ Seckill. Java │ │ │ │ │ SuccessKilled. Java │ │ │ │ │ │ │ │ │ ├ ─ enums │ │ │ │ │ SeckillStatEnum. Java │ │ │ │ │ │ │ │ │ ├ ─ interceptor │ │ │ │ │ MyAdapter. Java │ │ │ │ │ │ │ │ │ └ ─ redis │ │ │ │ │ ├─ DistributedLock │ │ ├─ Redis │ │ │ ├─ DistributedLock │ │ ├─ RedissLockDemo │ │ │ │ │ RedissLockUtil. Java │ │ │ │ │ RedissonAutoConfiguration. Java │ │ │ │ │ RedissonProperties. Java │ │ │ │ │ │ │ │ │ └ ─ zookeeper │ │ │ │ ZkLockUtil. Java │ │ │ │ │ │ │ ├ ─ queue │ │ │ │ ├ ─ the JVM │ │ │ │ │ SeckillQueue. Java │ │ │ │ │ TaskRunner. Java │ │ │ │ │ │ │ │ │ ├ ─ kafka │ │ │ │ │ KafkaConsumer. Java │ │ │ │ │ KafkaSender. Java │ │ │ │ │ │ │ │ │ └ ─ redis │ │ │ │ RedisConsumer. Java │ │ │ │ RedisSender. Java │ │ │ │ RedisSubListenerConfig. Java │ │ │ │ │ │ │ ├ ─ repository │ │ │ │ SeckillRepository. Java │ │ │ │ │ │ │ ├ ─ service │ │ │ │ │ ISeckillDistributedService. Java │ │ │ │ │ ISeckillService. Java │ │ │ │ │ │ │ │ │ └ ─ impl │ │ │ │ SeckillDistributedServiceImpl. Java │ │ │ │ SeckillServiceImpl. Java │ │ │ │ │ │ │ └ ─ web │ │ │ SeckillController. Java │ │ │ SeckillDistributedController. Java │ │ │ │ │ ├ ─ resources │ │ │ │ Application. Properties │ │ │ ├─ Application. Properties │ │ │ │ ├─ SQL │ │ │ ├─static │ │ ├─ application └ ─ templates │ │ └ ─ webappCopy the code

Thinking to improve

  • How to prevent a single user to repeat the second kill order?
  • How do I prevent malicious calls to a seckill interface?
  • What if the user kills successfully and never pays?
  • After the message queue processing is complete, if the asynchronous notification to the user is successful?
  • How to ensure the normal running (high availability) of Redis, Zookeeper, and Kafka services?
  • How to ensure that the second kill service does not affect other services under high concurrency (isolation)?

Code cloud download: build distributed kill system from 0 to 1

For reference

SpringBoot development case for integrating Kafka message queues

Author: Xiao Qi 2012

blog.52itstyle.com