做电商网站,经常会有各种秒杀和热门商品,所以高并发的处理一直是电商最重要的事情。这里记录下当初自己是如何处理的
写在前面:
1、本文设计到的并发处理均是针对纵向,不针对横向扩展,即只设计从PHP层面到数据库层面的处理,不涉及多台服务器,集群、大带宽等的横向设计。
2、本文中涉及到的高并发并不是淘宝京东等几百万几千万等的高并发,仅仅只是普通最多上万的并发处理
3、本文不对悲观锁乐观锁做设计
问题:
普通电商中的秒杀中的并发问题,超卖问题
实例:商品数量为100,秒杀人数为10000,整点开始秒杀
秒杀大概流程:
①商品详情点击购买(秒杀)--》②输入信息提交订单--》③进行支付
解决思路:
1、人数阀门设计
2、会员排队设计
3、问答问题设计
4、库存缓存设计
5、页面静态设计
思路理解:
一、人数阀门设计:进行用户人群过滤。
商品数量只有100份,秒杀人数有10000人,那么我们就设计1道阀门(根据情况,可以设计3道或者2道都可以的)。
在整点的时候,我们对点击了“购买”按钮后,我们只运行500人进入信息填写页面,信息填写完成后提交订单。效果如下:
①商品详情点击购买(秒杀)--》②输入信息提交订单--》③进行支付
10000人 500人 (这里也可以设计阀门,只允许多少人进入支付)
其他未进入的如何处理乃?显示已抢完或者排队等待(这就是后面要提到的排队系统设计)。
二、会员排队设计:对用户进行排队,排在前面的先购买
这相当于是消息队列模式了,如果秒杀是立即知道结果,排队可能会有点鸡肋。
在第二步②输入信息提交订单后进行排队,排在前面的先购买,排在后面的后购买
三、问答问题设计:过滤掉一些反应慢的用户
在第一步①点击购买后跳转到问题页面,用户必须回答正确问题后,方可进入后面的流程
四、库存缓存设计:缓存库存,判断用户购买的商品是否还有,不读取数据库,速度快,也不会增加数据库负担,经过前面的过滤,超卖的可能性比较低了
提前将商品库存缓存起来,到下单购买的时候,用户购买了就减1,每次都通过库存缓存判断一下,如果为0就显示已抢完。
五、页面静态设计:尽量静态缓存化【CDN那些这里不做考虑】
第一步①商品详情页面,尽量进行缓存,减轻大批量用户在访问商品页面的时候,大量查询数据库。
问答问题页面:全静态,加载快,无数据库负担。
排队等待页面:全静态,加载快,无数据库负担。
排队结束页面:全静态,加载快,无数据库负担。
小试牛刀:
上面说了那么多废话,总归在一起,流程大概就成了下面这样:
①商品详情点击购买(秒杀) --》 ②进入问题回答页面 --》③排队等待 --》 ④输入信息提交订单 --》 ⑤进行支付
页面缓存 问题过滤 阀门过滤 缓存库存减少
页面缓存 页面缓存
实战:
代码怎么独立出来乃?还是自己写一个流程。今天先到这里吧,代码实战再单独写一个
附件:
一、参考资料
①、淘宝阀门设计
②、各大电商网站秒杀流程
③、网络上面的各种文献资料
写在最后:
本人对并发处理并不深入,所知道的知识都是来源于网络资料和各种网站参考。虽然我这样的设计已经用于系统中,并且基本上解决了普通的这些问题,但是这样的设计可能存在一定的问题或者不完善,或者根本就是错误的。
我希望有对这方面很了解的大神希望给出点评和指导,我好学习和交流,非常感谢!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor