惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 飞翔在天

【转载】技术方案设计的方法论及案例分享—如何体现技术深度 云原生 性能调优 团队管理 技术管理 【生产案例】日登录用户、月登录用户统计、聚合统计 【生产案例】点赞数、排行榜、未读消息数(含B站方案) - 飞翔在天 【JVM】类加载器&双亲委派 【架构】单元化架构 【配置项读取】 如何写好代码 【缓存】基本理论 Lambda架构和Kappa架构 【架构-案例】聊天系统 【Spring】整体 【JVM】 【Spring-WebFlux】响应式 AI ServiceComb
【生产案例】
飞翔在天 · 2025-06-12 · via 博客园 - 飞翔在天

问题:高并发下如何防止商品超卖?  https://www.cnblogs.com/12lisu/p/18908447

方案1:数据库乐观锁

  优点:实现简单、无需额外中间件 
  缺点: 可能出现大量更新失败、高并发时DB压力大

方案2:Redis原子操作

  Redis原子操作的核心原理是使用:Redis + Lua脚本。

  扣减成功后写入消息队列;扣减失败返回秒杀失败

  性能对比:

  • 单节点QPS:数据库方案500 vs Redis方案8万
  • 响应时间:<1ms vs 50ms+
// Lua脚本保证原子性
String lua = "if redis.call('get', KEYS >= ARGV[1] then " +
             "return redis.call('decrby', KEYS[1], ARGV " +
             "else return -1 end";

public boolean preDeduct(String itemId, int count) {
    RedisScript<Long> script = new DefaultRedisScript<>(lua, Long.class);
    Long result = redisTemplate.execute(script, 
        Collections.singletonList(itemId), count);
    return result != null && result >= 0;
}

方案3: 分布式锁

  最常用的分布式锁的方案是Redisson

  注意事项

  1. 1.锁粒度要细化到商品级别
  2. 2.必须设置等待时间和自动释放
  3. 3.配合异步队列使用效果更佳

方案4:消息队列削峰

   可以使用 RocketMQ的事务消息

  技术指标

  • 削峰能力:10万QPS → 2万TPS
  • 订单处理延迟:<1秒(正常时段)
// RocketMQ事务消息示例
TransactionMQProducer producer = new TransactionMQProducer("stock_group");
producer.setExecutor(new TransactionListener() {
    @Override
    public LocalTransactionState executeLocalTransaction(Message msg) {
        // 扣减数据库库存
        return LocalTransactionState.COMMIT_MESSAGE;
    }
});

方案5:预扣库存

预扣库存是防止商品超卖的终极方案。用户提交订单时,做的是reids中库存预扣,只有当实际支付完成后,才会做数据库层的库存扣减。

// Guava RateLimiter限流
RateLimiter limiter = RateLimiter.create(1000); // 每秒1000个令牌

public boolean preDeduct(Long itemId) {
    if (!limiter.tryAcquire()) return false;
    
    // 写入预扣库存表
    preStockDao.insert(itemId, userId);
    return true;
}

性能数据

  • 百万级并发支撑能力
  • 库存准确率99.999%
  • 订单处理耗时200ms内

其实在很多大厂中,一般会将防止商品超卖的多种方案组合使用。

架构图如下:

 通过组合使用:

  1. Redis做第一道防线(承受80%流量)
  2. 分布式锁控制核心业务逻辑
  3. 预扣库存+消息队列保证最终一致性

实战经验:某电商在2023年双11中:

  • Redis集群承载98%请求
  • 分布式锁拦截异常流量
  • 预扣库存保证最终准确性

系统平稳支撑了每秒12万次秒杀请求,0超卖事故发生!