Table of Contents
回复内容:
Home Backend Development PHP Tutorial 关于抢京东券高并发的问题?

关于抢京东券高并发的问题?

Jun 06, 2016 pm 08:18 PM
php flash sale High concurrency

之前在一个微信公众号上做了一个抢京东券的功能, 50 张京东券,面额 50 、 100 不等,存在一张 card 表中,四个字段, id , number , money , is_taken 。

因为之前没有这种高并发处理的经验,所以使用了一种最传统的方式来实现:

方案一:来一个人我就从数据库中取一张京东券出来给他,并将该京东券标记为已使用(即更新 is_taken 字段),并将该用户插入到 winner 表中

这个方案最终导致的悲剧是,有一张京东券被两个人领取到了。
我所理解会出现这个问题的原因是获取未被占用的京东券数据( select )和更新该条京东券数据( update )是两个独立的操作,在这两个操作之间存在时间间隔,例如 A 用户刚得到了一张 100 元的京东券,还未来得及更新, B 用户涌入查询到这张京东券未被使用,所以 B 用户也获得了这张京东券。

问题一:我这个理解是正确的吗?还是有更深入的原因?

出了这个问题后,我在网上查找关于高并发相关的资料,几乎都提到了队列和锁。就我个人了解队列可以使用 redis 或者 memcacheq(这个没用过,不熟悉),所以自己想了第二种方案。

方案二:事先将京东券 id 数据压入到 redis 的 list 中,每过来一个有效的用户,就 pop 一个 id 给他(当 pop 出来的数据为空时说明京东券已经被抢光),并将用户 id 与京东券 id 的对应关系存储到 redis 的 set 当中去,然后根据这个 id 来查找京东券数据,显示给用户京东券的面额,并将 set 中的数据存储到数据库当中去。

个人觉得这种方案会比第一种方案要好的多。但是没有真正意义上去实践过,只是个人思考的一个结果。

问题二:第二种方案是否可行?是否还有更优方案?或者说方案二是否有可以优化的地方?

问题三:在高并发时很多文章中说到的锁是一个怎样的概念呢?我的理解是这个锁就像是数据库的一个大门,一次只放一个人进去,是这样吗?具体该如何设计和使用?

问题四:在应对大流量高并发的情况时,在服务器层面要做哪些工作?

问题五:我所举得这个例子与平常类似网上商城中的秒杀功能有哪些相同和相异之处呢?是否可以按照方案二的设计思路进行设计呢?

回复内容:

之前在一个微信公众号上做了一个抢京东券的功能, 50 张京东券,面额 50 、 100 不等,存在一张 card 表中,四个字段, id , number , money , is_taken 。

因为之前没有这种高并发处理的经验,所以使用了一种最传统的方式来实现:

方案一:来一个人我就从数据库中取一张京东券出来给他,并将该京东券标记为已使用(即更新 is_taken 字段),并将该用户插入到 winner 表中

这个方案最终导致的悲剧是,有一张京东券被两个人领取到了。
我所理解会出现这个问题的原因是获取未被占用的京东券数据( select )和更新该条京东券数据( update )是两个独立的操作,在这两个操作之间存在时间间隔,例如 A 用户刚得到了一张 100 元的京东券,还未来得及更新, B 用户涌入查询到这张京东券未被使用,所以 B 用户也获得了这张京东券。

问题一:我这个理解是正确的吗?还是有更深入的原因?

出了这个问题后,我在网上查找关于高并发相关的资料,几乎都提到了队列和锁。就我个人了解队列可以使用 redis 或者 memcacheq(这个没用过,不熟悉),所以自己想了第二种方案。

方案二:事先将京东券 id 数据压入到 redis 的 list 中,每过来一个有效的用户,就 pop 一个 id 给他(当 pop 出来的数据为空时说明京东券已经被抢光),并将用户 id 与京东券 id 的对应关系存储到 redis 的 set 当中去,然后根据这个 id 来查找京东券数据,显示给用户京东券的面额,并将 set 中的数据存储到数据库当中去。

个人觉得这种方案会比第一种方案要好的多。但是没有真正意义上去实践过,只是个人思考的一个结果。

问题二:第二种方案是否可行?是否还有更优方案?或者说方案二是否有可以优化的地方?

问题三:在高并发时很多文章中说到的锁是一个怎样的概念呢?我的理解是这个锁就像是数据库的一个大门,一次只放一个人进去,是这样吗?具体该如何设计和使用?

问题四:在应对大流量高并发的情况时,在服务器层面要做哪些工作?

问题五:我所举得这个例子与平常类似网上商城中的秒杀功能有哪些相同和相异之处呢?是否可以按照方案二的设计思路进行设计呢?

我估计你的方案一是这样子的

<code>select * from card where is_token=0 limit 1;
//有结果时,继续执行以下语句
update card set is_token=1 where id=xx;
insert into winner ...</code>
Copy after login

改进

<code>select * from card where is_token=0 limit 1;
//有结果时,继续执行以下语句
update card set is_token=1 where id=xx and is_token=0;
//获取影响行数,为0则失败,否则执行 insert into winner ...</code>
Copy after login

方案二
可行。
因为你的业务逻辑比较简单,也可以使用redis的计数器
即当计数器

问题三 锁
悲观锁(Pessimistic Lock)
顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁。传统的关系型数据库里边就用到了很多这种锁机制,比如行锁,表锁等,读锁,写锁等,都是在做操作之前先上锁。

<code>begin;
select * from card where is_token=0 limit 1 for update;
</code>
Copy after login

乐观锁
顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号等机制。参考改进后的方案一。

乐观锁用的比较多,使用redis的watch

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles