PHP+Redis事务解决高并发下商品超卖问题

2023-05-2818:14:47后端程序开发Comments647 views字数 1947阅读模式

对于一些有一定用户量的电商网站,如果只是单纯的使用关系型数据库(如MySQL、Oracle)来做抢购,对数据库的压力是非常大的,而且如果不使用好数据库的锁机制,还会导致商品、优惠券超卖的问题。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

我所在的公司也遇到了同样的问题,问题发生在优惠券被超量抢购上,在问题发生后我们开始想办法解决问题,由于自己使用redis比较多,我准备使用redis来解决这个问题。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

利用redis的高性能和事务特性来解决线上优惠券被超库存抢购的问题,下面我给出我临时解决这个问题的第一版的伪代码,去掉了一些细节:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

/**文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

 * 抢优惠券(秒杀)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

 * @param int $couponId 商品ID文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

 * @param int $uid 用户ID文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

 * @return bool文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

 */文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

function secKill($couponId$uid)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//1.初始化Redis连接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$redis = new Redis();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

if (!$redis->connect('127.0.0.1', 6379)) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

trigger_error('Redis连接出错!!!', E_USER_ERROR);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

else {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

echo '连接正常<br>';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//秒杀商品的库存key文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$key = 'secKill:'.$couponId.':stock';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$redis->watch($key);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//获取库存文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$stock = $redis->get($key);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//秒杀未开始,表示库存为null文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

if (!$stock && !is_numeric($stock)) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

echo '秒杀未开始';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

return false;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//判断库存,如果库存大于0,则减库存,将该成功秒杀用户加入哈希表,如果小于等于0,秒杀结束文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

if ($stock <= 0) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

echo '秒杀已结束';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

return false;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//用户已经成功秒杀过一次了,不允许再次参与秒杀文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

if ($redis->sIsMember('secKill:'.$couponId.':uid'$uid)) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

echo '秒杀失败';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

return false;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//代码走到这里,说明该用户是第一次参与秒杀,将库存减一,然后把这个人放到已抢到的集合表文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//multi(),返回一个redis对象,并进入multi-mode模式,一旦进入multi-mode模式,以后调用的所有方法都会返回相同的对象,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//直到exec()方法被调用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$result = $redis->multi()->decr($key)->sAdd('secKill:'.$couponId.':uid'$uid)->exec();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

if (emptyempty($result)) {//事务被取消文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

echo '秒杀失败';文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

return false;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

//抢券成功,将优惠券ID和UID放入到队列中,由一个单独的进程队列来消费队列里的数据,向用户推送抢到的优惠券文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$redis->lPush('couponOrder'$couponId.'+'.$uid);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$redis->close();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

return true;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$couponId = 11211;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

$uid = mt_rand(1, 100);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

secKill($couponId$uid);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

首先,我模拟设置优惠券ID为11211的优惠券库存为10个。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

PHP+Redis事务解决高并发下商品超卖问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

然后,我们使用ab工具来模拟1000次请求,50并发量来测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

ab -n 1000 -c 50 www.test.com/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

然后我们通过Redis Desktop Manager来查看一些Redis的结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

couponOrder队列里已经有了10个用户的信息了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

PHP+Redis事务解决高并发下商品超卖问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

并且优惠券的剩余数量也是0了,不再是负数了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

PHP+Redis事务解决高并发下商品超卖问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

同时,用户抢券集合里也保存了10个用户的UID信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

PHP+Redis事务解决高并发下商品超卖问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

上面这串代码解决了两个问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

解决了瞬时的大量查询到数据库上给数据库造成很大压力的问题,流量都被拦截在了redis缓存层文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

解决了优惠券被超库存抢购的问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

但是,这段代码也存在一定的问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

没有使用redis连接池,频繁创建新的redis有一定的性能影响文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

由于使用了事务,每一次并发请求中只会有一个用户抢券成功,该并发请求中的其它用户都会失败,只能等第二次并发文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

同样还是事务导致的库存遗留问题,如果有10个商品,1000次请求每次200并发量,5次并发请求就完成了1000次请求,但是只会有5个用户成功抢到,如果没有后续的请求,会导致库存还有5份存量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

提示:在消费队列里,如果优惠券发放失败,一定要立即记录并短信通知运营管理人员,看看是否能重发或者通过后台手动定向推送给用户。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

所以,后续我又使用了lua脚本和redis配合一起来解决了这个问题。具体代码,我会后续整理处理补充完整。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/42937.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/42937.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定