guozhaoran / spikesystem Goto Github PK
View Code? Open in Web Editor NEW一个秒杀系统的例子分析
一个秒杀系统的例子分析
大佬你好,我是Go语言中文网(https://studygolang.com )站长,看到你这个项目,很赞,我想分享给更多的 Go 语言爱好者。特向你申请授权我发布到网站和对应的公众号等渠道。因为会大量引用README中的内容,期待您的回复!谢谢!
感觉最后可以加一步,当余票很少时,也许把用户id本地hash,少于多少(比如还有10张票,QPS1w,用hash值少于多少卡掉99%的请求,使qps就100(为余票个数*10)就行)的直接返空,直接显示售罄了,这样让大部分本地库存不用再去请求redis远程减库存。
如果分库server的库存减和总redis库存减的操作是原子操作这个是前提的话,8点放票,此时库存都是足够的,瞬间所有的分仓库都会去请求Redis。100W并发请求,就可能有绝大多数的请求同样也会去Redis。 这个问题如何解决。单单去增加redis服务器的并发能力吗?
readme里lua脚本
if(ticket_sold_nums > ticket_total_nums) then
return redis.call('HINCRBY', ticket_key, ticket_sold_key, 1)
end
应该是
ticket_total_nums > ticket_sold_nums
代码里是对的
使用chan使得所有的请求在单实例中串行处理,是否会降低吞吐?
我理解本地库存最主要的目的,应该是当本地库存不足时,不去请求redis。保证本地库存源子操作这种需求源子操作或者用lock就可以解决,不必让所有请求的串行处理吧。
而且go chan底层也是基于lock实现的,通过benchmark chan 没体现出任何优势。
如果理解错误点 还望指正。
1、如何临时追加库存
2、扣费成功后,发送mq失败怎么处理?
3、mq可能存在丢消息的情况;
4、redis数据可能存在丢失,如何处理?
lua 脚本 >= 情况下会超卖一个,所以改成>
if localSpike.LocalDeductionStock() && remoteSpike.RemoteDeductionStock(redisConn) {
util.RespJson(w, 1, "抢票成功", nil)
LogMsg = LogMsg + "result:1,localSales:" + strconv.FormatInt(localSpike.LocalSalesVolume, 10)
} else {
util.RespJson(w, -1, "已售罄", nil)
LogMsg = LogMsg + "result:0,localSales:" + strconv.FormatInt(localSpike.LocalSalesVolume, 10)
}
失败应该回滚本地的一个记录
if localSpike.LocalDeductionStock() && remoteSpike.RemoteDeductionStock(redisConn) {
util.RespJson(w, 1, "抢票成功", nil)
LogMsg = LogMsg + "result:1,localSales:" + strconv.FormatInt(localSpike.LocalSalesVolume, 10)
} else {
util.RespJson(w, -1, "已售罄", nil)
spike.LocalSalesVolume = spike.LocalSalesVolume - 1
LogMsg = LogMsg + "result:0,localSales:" + strconv.FormatInt(localSpike.LocalSalesVolume, 10)
}
把库存分配到本地机器的机制是怎样的,在什么时机或以什么方式进行,怎样保证数据的一致性,如果 一台机器分配时没有成功,这台机器没有了库存,落在此机器上的请求会不会买不到票?
对最后一张图不是很理解这个消息队列的操作时机
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.