forked from shishan100/Java-Interview-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
文亮
committed
Aug 14, 2019
1 parent
f9d8506
commit b6f1a05
Showing
3 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
《亿级流量电商详情页系统实战》,部署redis和zookeeper,redisson做分布式锁,curator做分布式锁,试一试 |
26 changes: 26 additions & 0 deletions
26
docs/distributed-system/highly-concurrent-distribute-lock.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
分段加锁 | ||
|
||
有好多同学出去面试,聊到分布式锁这块,都被人考察分布式锁能不能抗高并发的问题了 | ||
|
||
对某个商品下单,对一个分布式锁每秒突然有上万请求过来,都要进行加锁,此时怎么办呢?可能就会导致你 | ||
|
||
|
||
|
||
比如你的苹果库存有10000个,此时你在数据库中创建10个库存字段 | ||
|
||
一个表里有10个库存字段,stock_01,stock_02,每个库存字段里放1000个库存 | ||
|
||
此时这个库存的分布式锁,对应10个key,product_1_stock_01,product_1_stock_02 | ||
|
||
请求过来之后,你从10个key随机选择一个key,去加锁就可以了,每秒过来1万个请求,此时他们会对10个库存分段key加锁,每个key就1000个请求,每台服务器就1000个请求而已 | ||
|
||
|
||
万一说某个库存分段仅仅剩余10个库存了,此时我下订单要买20个苹果,合并扣减库存,你对product_1_stock_5,加锁了,此时查询对应的数据库中的库存,此时库存是10个,不够买20个苹果 | ||
|
||
你可以尝试去锁product_1_stock_1,再查询他的库存可能有30个 | ||
|
||
此时你就可以下订单,锁定库存的时候,就对product_1_stock_5锁定10个库存,对product_1_stock1锁定10个库存,锁定了20个库存 | ||
|
||
|
||
分段加锁 + 合并扣减 |