


PHP's method to solve blocking high-concurrency inventory prevention and control overstocking such as snap sales, flash sales, property grabs, and lottery draws
Here are some ideas that I think are more feasible:
Option 1: Use message
queue to implement
can be based on messages
queue such as MemcacheQ. The specific implementation plan is as follows Let’s express itFor example, if there are 100 tickets for users to grab, then they can put these 100 tickets in the cache and do not lock them when reading and writing. When the amount of concurrency is large, about 500 people may successfully grab tickets, so that requests after 500 can be directly transferred to the static page at the end of the event. It is impossible for 400 out of 500 people to get the goods. Therefore, only the first 100 people can purchase successfully according to the order in which they enter the
queue. The next 400 people will go directly to the event end page. Of course, entering 500 people is just an example. You can adjust the number yourself. The activity end page must use a static page, not a database. This reduces the pressure on the database.
Option 2: When there are multiple servers, it can be implemented in the form of offloading
Suppose there are m tickets, n product servers receive requests, and x requests are forwarded randomly by the routing server
Directly Allocate m/n tickets to each product server
Make
counter in the memory of each product server, for example, allow m/n*(1+0.1) people to come in. When the memory
counter is full: People who enter later will jump directly to the static page where the activity ends,
notify the routing server that it will no longer be routed to this server (this is worth discussing).
The m/n*(1+0.1) individuals who come in from all the product servers are then forwarded to a payment server and enter the payment process to see who is faster. There are few people at this time, so locking and so on is simple.
Option 3. If it is a single server, you can use Memcache lock to implement
product_key is the ticket key
product_lock_key is the ticket lock key
When product_key exists in memcached, all users can Enter the order process.
When entering the payment process, first store add(product_lock_key, “1″) in memcached,
If the return is successful, enter the payment process.
If it fails, it means that someone has already entered the payment process, and the thread waits for N seconds and performs the add operation recursively.
Option 4: Use file exclusive lock
When processing an order request, use flock to lock a file. If the lock fails, it means that other orders are being processed. At this time, either wait or directly prompt the user "Server is busy" "
This article is going to talk about the fourth solution. The approximate code is as follows
Blocking (waiting) mode:
<?php $fp = fopen("lock.txt", "w+"); if(flock($fp,LOCK_EX)) { //..处理订单 flock($fp,LOCK_UN); } fclose($fp); ?>
Non-blocking mode:
<?php $fp = fopen("lock.txt", "w+"); if(flock($fp,LOCK_EX | LOCK_NB)) { //..处理订单 flock($fp,LOCK_UN); } else { echo "系统繁忙,请稍后再试"; } fclose($fp); ?>
The above introduces PHP's ideas and methods to solve blocking high-concurrency inventory prevention and control overflows such as rush buying, flash sales, property grabbing, lottery, etc., including queues, flash sales, number of people, and counters. I hope friends who are interested in PHP tutorials can helped.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

After Jdk1.5, under the java.util.concurrent.locks package, there is a set of interfaces and classes for thread synchronization. When it comes to thread synchronization, everyone may think of the synchronized keyword, which is a built-in keyword in Java. It handles thread synchronization, but this keyword has many flaws and is not very convenient and intuitive to use, so Lock appears. Below, we will compare and explain Lock. Usually when we use the synchronized keyword, we will encounter the following problems: (1) Uncontrollability, unable to lock and release locks at will. (2) The efficiency is relatively low. For example, we are currently reading two files concurrently.

In Linux, there are several commonly used file locking commands, including flock, fcntl, lockfile, and flockfile. These commands are used to provide mutually exclusive access to files in a multi-process or multi-thread environment. Here is a detailed tutorial of these commands: flock command: flock command can be used in shell scripts to perform exclusive locking of files. Use the following syntax to lock a file: flock [options] filename command For example, to lock a file named file.txt and execute the command, you can run the following command: flockfile.txtls - The lflock command locks the file during the execution of the command and executes the command. The lock is automatically released after the command is completed. fcnt

1. Function (1) The Lock method to acquire locks supports interruption, no acquisition after timeout, and is non-blocking (2) It improves semantics. Where to lock and unlock must be written out (3) Lock explicit lock can bring us Comes with good flexibility, but at the same time we must manually release the lock (4) Support Condition condition object (5) Allow multiple reading threads to access shared resources at the same time 2.lock usage //Get the lock voidlock() //If the current thread has not If interrupted, acquire the lock voidlockInterruptibly()//Return a new Condition instance bound to this Lock instance ConditionnewCondition()//Lock only when called

Note 1. Lock is an interface under the java.util.concurent package, which defines a series of locking operation methods. 2. The Lock interface mainly includes ReentrantLock, ReentrantReadWriteLock, ReentrantReadWriteLock, and WriteLock implementation classes. Different from Synchronized, Lock provides related interfaces such as acquiring locks and releasing locks, making it more flexible to use and more complex to operate. InstanceReentrantReadWriteLocklock=newReentrantReadWriteLock();Lockread

1. Multi-environment switching is implemented in SpringBoot. In SpringBoot, in addition to application.properties, the file names of other configuration files we create need to meet the format of application-{profile}.properties, where {profile} corresponds to your environment identifier (not necessarily It is a .properties file or .yml) and its corresponding {profile} value is customized by the developer (such as dev, product). When the project is started, you only need to add the corresponding parameters and springboot will read it. Time to profile. Specific profile configuration

Summary: The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, we will discuss this issue together. The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, let’s discuss it together

1. The acquisition methods lock(), tryLock(), tryLock(longtime, TimeUnitunit) and lockInterruptibly() are all used to acquire locks. (1) The lock() method is the most commonly used method, which is used to obtain locks. If the lock has been acquired by another thread, wait. (2) The tryLock() method has a return value, which means it is used to try to acquire the lock. If the acquisition is successful, it returns true. If the acquisition fails (that is, the lock has been acquired by another thread), it returns false, which means this The method returns immediately no matter what. You won't be waiting there when you can't get the lock. (3) tryLoc
