Home Backend Development PHP Tutorial PHP并发、超卖处理

PHP并发、超卖处理

Jun 20, 2016 pm 12:46 PM

做电商网站,经常会有各种秒杀和热门商品,所以高并发的处理一直是电商最重要的事情。这里记录下当初自己是如何处理的

写在前面:

1、本文设计到的并发处理均是针对纵向,不针对横向扩展,即只设计从PHP层面到数据库层面的处理,不涉及多台服务器,集群、大带宽等的横向设计。

2、本文中涉及到的高并发并不是淘宝京东等几百万几千万等的高并发,仅仅只是普通最多上万的并发处理

3、本文不对悲观锁乐观锁做设计

问题:

普通电商中的秒杀中的并发问题,超卖问题

实例:商品数量为100,秒杀人数为10000,整点开始秒杀

秒杀大概流程:

①商品详情点击购买(秒杀)--》②输入信息提交订单--》③进行支付

解决思路:

1、人数阀门设计

2、会员排队设计

3、问答问题设计

4、库存缓存设计

5、页面静态设计

思路理解:

一、人数阀门设计:进行用户人群过滤。

商品数量只有100份,秒杀人数有10000人,那么我们就设计1道阀门(根据情况,可以设计3道或者2道都可以的)。

在整点的时候,我们对点击了“购买”按钮后,我们只运行500人进入信息填写页面,信息填写完成后提交订单。效果如下:

①商品详情点击购买(秒杀)--》②输入信息提交订单--》③进行支付

               10000人                          500人                    (这里也可以设计阀门,只允许多少人进入支付)

其他未进入的如何处理乃?显示已抢完或者排队等待(这就是后面要提到的排队系统设计)。

二、会员排队设计:对用户进行排队,排在前面的先购买

这相当于是消息队列模式了,如果秒杀是立即知道结果,排队可能会有点鸡肋。

在第二步②输入信息提交订单后进行排队,排在前面的先购买,排在后面的后购买

三、问答问题设计:过滤掉一些反应慢的用户

在第一步①点击购买后跳转到问题页面,用户必须回答正确问题后,方可进入后面的流程

四、库存缓存设计:缓存库存,判断用户购买的商品是否还有,不读取数据库,速度快,也不会增加数据库负担,经过前面的过滤,超卖的可能性比较低了

提前将商品库存缓存起来,到下单购买的时候,用户购买了就减1,每次都通过库存缓存判断一下,如果为0就显示已抢完。

五、页面静态设计:尽量静态缓存化【CDN那些这里不做考虑】

第一步①商品详情页面,尽量进行缓存,减轻大批量用户在访问商品页面的时候,大量查询数据库。

问答问题页面:全静态,加载快,无数据库负担。

排队等待页面:全静态,加载快,无数据库负担。

排队结束页面:全静态,加载快,无数据库负担。

小试牛刀:

上面说了那么多废话,总归在一起,流程大概就成了下面这样:

①商品详情点击购买(秒杀)  --》 ②进入问题回答页面      --》③排队等待 --》 ④输入信息提交订单    --》  ⑤进行支付

    页面缓存                                        问题过滤                      阀门过滤                                       缓存库存减少

                                                          页面缓存                      页面缓存

实战:

代码怎么独立出来乃?还是自己写一个流程。今天先到这里吧,代码实战再单独写一个


附件:

一、参考资料

①、淘宝阀门设计

②、各大电商网站秒杀流程

③、网络上面的各种文献资料


写在最后:

本人对并发处理并不深入,所知道的知识都是来源于网络资料和各种网站参考。虽然我这样的设计已经用于系统中,并且基本上解决了普通的这些问题,但是这样的设计可能存在一定的问题或者不完善,或者根本就是错误的。

我希望有对这方面很了解的大神希望给出点评和指导,我好学习和交流,非常感谢!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles