Objective
Recently, was pushed to send a lot of seconds to kill the structure of the article, sneak up on their own also summed up the Internet platform of the second kill architecture design, of course, also borrowed a lot of students thinking. As the saying goes, out of the case of the architecture are bullying, the end of the use of springboot simulation to achieve a partial second kill scene, at the same time share with you to exchange.
Second kill scene
The second kill scene is just a lot of users at the same time snapping up one or more items, the special vocabulary is called high concurrency. The reality is often loved by the scene, a group of Aunt snapping discount eggs will not be unfamiliar, so the scene let waiter elder sister very helpless, catch up with no money.
Business Features
- Instant high concurrency, the computer next to the small brother, xxx sisters such as supermarket looting Aunt General, Crazy Point mouse
- Less inventory, cheap, scarce limited, it is worth buying, such as Apple kidney, rice flour, hammer powder (understanding Viva)
User size
The size of the user can be large and small, hundreds of or thousands of people of the active monomer structure is enough to cope with, simple locking, in-process queue can be easily done. Once you rise to the million, the scale of the Tens should consider distributed clusters to deal with instantaneous high concurrency.
Second Kill schema
Architecture Hierarchy
General business in doing activities, often encounter a variety of malicious DDoS attacks (the use of innocent people to eat melon to seize resources), leading to the real we can not get services! So high anti-IP is still very necessary.
Activity means more people, access to SLB, multi-cloud servers for traffic distribution, through the distribution of traffic to extend the external service capabilities of the application system, by eliminating the single point of failure to improve the availability of the application system.
Based on the SLB price and flexibility considerations, we access the Nginx to do current limit distribution, to ensure the normal operation of back-end services.
- Backend second-kill business logic, based on Redis or Zookeeper distributed lock, Kafka or Redis to do Message Queuing, DRDs database middleware to achieve data read and write separation.
Optimization ideas
Diversion, diversion, diversion, the important thing to say three times, and then a great machine can not withstand high-level concurrency.
Current limit, current limit, current limit, after all, the second kill Goods Limited, anti-brush under the premise of no absolute fairness, according to the load capacity of each service, set the flow limit.
Cache, cache, cache, try not to get a large number of requests through the DB layer, before the event begins, the commodity information can be pushed to the distributed cache.
Asynchronously, asynchronously, asynchronously, analyzes and identifies the logic that can be processed asynchronously, such as logs, to shorten the system response time.
Master preparation, master preparation, master preparation, if the condition is well-prepared for the main disaster recovery plan is also very necessary (refer to a year hammer activity was attacked).
- Finally, in order to support the higher concurrency, the pursuit of better performance, the deployment model of the server can be optimized, part of the request to take the normal second kill process, some requests directly return the second kill failure, the disadvantage is that the development of the deployment needs to maintain two sets of logic.
Tiered optimization
- Front-end optimization: Generate static product page push cache and CDN before activity starts, static file (JS/CSS) request pushes to file server and CDN.
- Network optimization: If it is a national user, preferably a BGP multi-line room, reduce network latency.
- Application Service Optimization: Nginx best configuration, Tomcat connection pool optimization, database configuration optimization, database connection pool optimization.
Full-Link pressure measurement
- Analysis of business scenarios involving systems requiring pressure measurement
- Coordinate the resources of each pressure measurement system and set up the pressure measurement environment
- Measurement data isolation and monitoring (response time, throughput, error rate, and other data are displayed in real time as a chart)
- Statistics of the measured results (average response time, average throughput, and so on are shown in chart form at the end of the test)
- Optimize individual system performance, associated processes, and entire business processes
The entire pressure measurement optimization process is a continuous optimization of continuous improvement of the process, in advance through testing to find problems, optimize the system, avoid problems, designated contingency plan, in order to make the stability and performance of the system can be improved quality.
Code case
Possible second-kill architecture principle we all understand, there are many ways to realize the Internet, but most of the text is a description of how to tell you how, what locking, Cache, queue and so on. But few comprehensive cases tell you how to do it, since it is from 0 to 1, hopefully the following code case can help you. Of course, the final implementation of the production, there is a long way to go, according to their own business code, implementation and deployment.
You will learn the following knowledge in the code case (not regularly replenished):
- How everyone springboot Micro Services
- Use of the threadpoolexecutor thread pool
- Usage scenarios for Reentrantlock and synchronized
- Database lock mechanism (pessimistic lock, optimistic lock)
- Distributed Locks (Redisslock, Zookeeper)
- In-process Message Queuing (Linkedblockingqueue, Arrayblockingqueue, Concurrentlinkedqueue)
- Distributed Message Queuing (Redis, Kafka)
Code structure:
├─src│├─main││├─java│││└─com│││└─itstyle│││└─seckill││││applicatio N.java│││││││├─common││││├─api│││││swa Ggerconfig.java│││││││││├─config│││││indexcontrol Ler.java│││││││││├─dynamicquery│││││dynamicqu Ery.java│││││dynamicqueryimpl.java│││││nativequeryresultentity.java│ ││││││││├─entity│││││result.java│││ ││seckill.java│││││successkilled.java││││││││ │├─enums│││││seckillstatenum.java│││││││││├ ─interceptor│││ ││myadapter.java│││││││││└─redis││││ redisconfig.java││││redisutil.java│││││││├─ Distributedlock││││├─redis│││││redisslockdemo.java│││││ Redisslockutil.java│││││redissonautoconfiguration.java│││││rediss Onproperties.java│││││││││└─zookeeper││││zklocku Til.java│││││││├─queue││││├─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│││seckillcont Roller.java│││seckilldistributedcontroller.java│││││├─resources│ │││application.properties││││logback-spring.xml│││││││├─sql││││seckill.sql│││ ││││├─statiC│││└─templates││└─webapp
Thinking about improvement
- How can I prevent a single user from repeating a single second?
- How to prevent malicious calls to kill the interface?
- What if the user kills successfully and never pays?
- After the message queue processing is complete, if the asynchronous notification to the user The second kill succeeds?
- How can I guarantee the normal operation of Redis, Zookeeper, Kafka services (high availability)?
- How does high concurrency next-second kill business do not affect other businesses (isolation)?
Code Cloud Download: Build a distributed second-kill system from 0 to 1
Available for reference
Enterprise Cloud resolution DNS
Load balancing of Nginx learning
Nginx Learning Cache Configuration
http/2.0 Configuration of Nginx Learning
How Nginx learns to prevent traffic attacks
Integration of Springboot Development case Kafka implement Message Queuing
Springboot development case from 0 to 1 build a distributed second-kill system