Skip to content
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

Closed
wenbochang888 opened this issue May 18, 2018 · 7 comments
Closed

关于异步队列的问题 #1

wenbochang888 opened this issue May 18, 2018 · 7 comments

Comments

@wenbochang888
Copy link

你好,这是你的优化思路:

  1. 系统初始化,把商品库存数量加载到Redis
  2. 收到请求,Redis预减库存,库存不足,直接返回,否则进入3
  3. 请求入队,立即返回排队中
  4. 请求出队,生成订单,减少库存
  5. 客户端轮询,是否秒杀成功

第二点,redis预减库存。
经过我的多次实验, redis为单线程,并且减1的操作为原子操作。
redis.opsForValue().increment(key, -1)
那样肯定不会出现超卖的现象。
那么为何还要放到队列异步处理呢?直接处理不是更好,反正都要访问mysql数据库的,
放到队列不是多此一举嘛

期待你的回答,谢谢

@lukeewu
Copy link

lukeewu commented May 18, 2018 via email

@wenbochang888
Copy link
Author

@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个请求么?

@lukeewu
Copy link

lukeewu commented May 18, 2018 via email

@wenbochang888
Copy link
Author

@lukeewu
hahah,小米那个另当别论哈

你只是100个,假如是0.00001秒来了一百个,那么。。。

我想问下这个什么意思呢?我测试了好多遍,上面那段代码并不会出现超卖现象

@lukeewu
Copy link

lukeewu commented May 18, 2018 via email

@wenbochang888
Copy link
Author

@lukeewu

mysql依然要在0.000001秒内接收到100次请求

哈哈哈,这个还是很有难度的。
我用的是jmeter测试的,一秒模拟了1w了线程,没有出现问题。
还有待讨论,感谢你的回答

@lukeewu
Copy link

lukeewu commented May 18, 2018 via email

@b2stry b2stry closed this as completed Jan 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants