-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关于异步队列的问题 #1
Comments
因为redis非常快,一秒钟可以10万次,但是不用队列,那么一秒钟就会有10万个去请求mysql,那么那么依然无法保证数据不出错。所以加入了队列就会按顺序去消费,一个个来。只是时间会久一点,但是对于用户来说无所谓。因为队列是为了保证mysql的数据安全。
发自我的 iPhone
在 2018年5月18日,19:36,Egg <notifications@github.com<mailto:notifications@github.com>> 写道:
你好,这是你的优化思路:
1. 系统初始化,把商品库存数量加载到Redis
2. 收到请求,Redis预减库存,库存不足,直接返回,否则进入3
3. 请求入队,立即返回排队中
4. 请求出队,生成订单,减少库存
5. 客户端轮询,是否秒杀成功
第二点,redis预减库存。
经过我的多次实验, redis为单线程,并且减1的操作为原子操作。
redis.opsForValue().increment(key, -1)
那样肯定不会出现超卖的现象。
那么为何还要放到队列异步处理呢?直接处理不是更好,反正都要访问mysql数据库的,
放到队列不是多此一举嘛
期待你的回答,谢谢
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#1>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AiNd0LWvkRgs8vBlGz1CJfPcVv4yWhhIks5tzrIzgaJpZM4UElSL>.
|
` public String execute() {
如上代码 |
当然可以处理,但是你这个本质上已经无法保证绝对的数据安全了,你只是100个,假如是0.00001秒来了一百个,那么。。。另外秒杀也不单单是一百个,小米手机也是秒杀号称30万台,几秒钟没了。
发自我的 iPhone
在 2018年5月18日,19:58,Egg <notifications@github.com<mailto:notifications@github.com>> 写道:
@lukeewu<https://github.com/lukeewu>
` public String execute() {
long num = redis.opsForValue().increment(key, -1);
if (num <= 0) {
return "秒杀失败";
} else {
//入队
mq.send(new HappyLock(key, Integer.parseInt(num + "")));
}
return "";
}`
如上代码
假设有10w个请求,但商品数量只有100件(或者更多)。我redis预减少,屏蔽掉了9.9k个无效请求(if else那里判断了一下),难道mysql还处理不了这100个请求么?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AiNd0MwPtnTj9bO0KkDziwT0YEg2bEBAks5tzrd6gaJpZM4UElSL>.
|
@lukeewu
我想问下这个什么意思呢?我测试了好多遍,上面那段代码并不会出现超卖现象 |
如果0.000001秒一万个用户抢一百个商品,那么有一百个通过了,但是mysql依然要在0.000001秒内接收到100次请求,这个时候mysql可能会炸了的。你多次测试那么你模拟一百个线程同时跑才行,你如果用循环那是没有用的。你需要模拟一百个用户在极短时间内请求。而且就算你模拟了一百个用户,你让他们在同一微秒或者毫秒都同事请求那也是不太准的。这个系统设计的就是要保证数据库绝对安全。你如果把电商的秒杀排除了,那么这个系统就没有意义了,本来就是设计给电商的。你几个商品,一百个人去抢,不用队列,不用redis也不怕,假如说真的超卖了,那赶紧图买彩票吧。会中奖的。
发自我的 iPhone
在 2018年5月18日,20:15,Egg <notifications@github.com<mailto:notifications@github.com>> 写道:
@lukeewu<https://github.com/lukeewu>
hahah,小米那个另当别论哈
你只是100个,假如是0.00001秒来了一百个,那么。。。
我想问下这个什么意思呢?我测试了好多遍,上面那段代码并不会出现超卖现象
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AiNd0L9Cae7TVLz4Ch7jWafm5Hg5s3G2ks5tzrtJgaJpZM4UElSL>.
|
哈哈哈,这个还是很有难度的。 |
不客气!
发自我的 iPhone
在 2018年5月18日,20:28,Egg <notifications@github.com<mailto:notifications@github.com>> 写道:
@lukeewu<https://github.com/lukeewu>
mysql依然要在0.000001秒内接收到100次请求
哈哈哈,这个还是很有难度的。
我用的是jmeter测试的,一秒模拟了1w了线程,没有出现问题。
还有待讨论,感谢你的回答
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AiNd0H1ROX0ZGeEpAfpbE8AKBCcb_aVYks5tzr5pgaJpZM4UElSL>.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,这是你的优化思路:
第二点,redis预减库存。
经过我的多次实验, redis为单线程,并且减1的操作为原子操作。
redis.opsForValue().increment(key, -1)
那样肯定不会出现超卖的现象。
那么为何还要放到队列异步处理呢?直接处理不是更好,反正都要访问mysql数据库的,
放到队列不是多此一举嘛
期待你的回答,谢谢
The text was updated successfully, but these errors were encountered: