一个极简高效的秒杀系统(战略设计篇)
设置活动、活动商品、活动库存、开始结束时间等配置
系统需为每场活动分配全局唯一活动id:若提交的数据带有活动id,则表示更新
{
"activityName": "双十一秒杀第一场",
"startTime": 1540174800000, // 活动开始时间
"endTime": 1541988000000, // 活动结束时间
"itemLine": [
{
"itemId": 123, // 活动商品id
"itemType": 7, // 活动商品类型
"itemTitle": "这是商品标题",
"subTitle": "这是商品副标题",
"itemImage": "这是图片链接",
"salePrice": 66800, // 商品原价(单位:分)
"activityPrice": 100, // 活动价
"quota": 3, // 单个用户商品抢购件数限制
"stock": 100 // 商品活动库存
}
],
"activityRuleConfigs": [ // 活动规则列表
{
"configKey": "city", // 城市规则:在规则列表的城市可看到活动
"configValue": "17,5,10,2,3,4,11"
}
]
}
{
"traceId": "2910c88d0d4f45d5fe299f0c5829d72c",
"code": "SERVICE_RUN_SUCCESS",
"msg": "服务运行成功",
"status": 10000,
"success": true
}
{
"traceId": "2910c88a0d4f45d5be290f0c5829d72c",
"success": true,
"status": 10000,
"msg": "OK",
"code": "SUCCESS",
"data": [
{
"activityId": 1,
"activityName": "双十一秒杀第一场",
"startTime": 1541901600000,
"endTime": 1741951999000,
"enabled" true
}
]
}
{
"traceId": "889924ef8e6241a7a766107f38c5e0c0",
"success": true,
"status": 10000,
"msg": "OK",
"code": "SUCCESS",
"data": {
"activityId": 1,
"activityName": "双十一秒杀第一场",
"startTime": 1541901600000,
"endTime": 1741951999000,
"enabled" true
"items": [
{
"itemId": 53725,
"itemType": 1,
"itemTitle": "x商品",
"subTitle": "x商品副标题",
"itemImage": "http://img.xxxx.com/static/do1_QtSq1m2xM7VL6zEI4sUH",
"itemPrice": 19800,
"activityPrice": 4800,
"quota": 3,
"stock": 50,
"sold": 0
},
{
"itemId": 53724,
"itemType": 1,
"itemTitle": "y商品",
"subTitle": "y商品副标题",
"itemImage": "http://img.xxxx.com/static/MrcNjUeeoOG24zZH7nR.png",
"itemPrice": 42800,
"activityPrice": 17000,
"quota": 3,
"stock": 50,
"sold": 0
}
]
}
}
{
"traceId": "a77edf653da644959d331b7b55607958",
"success": true,
"status": 10000,
"msg": "OK",
"code": "SUCCESS",
"data": {
"itemId": 53724,
"itemType": 1,
"itemTitle": "x商品",
"subTitle": "商品副标题",
"itemImage": "http://img.xxxx.com/static/do1_QtSq1m2xM7VL6zEI4sUH",
"itemPrice": 42800,
"activityPrice": 17000,
"quota": 3,
"stock": 50,
"sold": 0,
"activity": {
"activityId": 1,
"activityName": "双十一秒杀第一场",
"startTime": 1541901600000,
"endTime": 1741951999000,
"enabled" true
}
}
}
{
"activityId": 1,
"buyerId": "buyer_001",
"itemId": 53724,
"orderId": "20191111123456789",
"orderTime": 1541901700000,
"quantity": 1
}
{
"traceId": "f689852f113e413d9940ce24020e7083",
"success": true,
"status": 10000,
"msg": "OK",
"code": "SUCCESS",
"data": true
}
{
"activityId": 1,
"orderId": "20191111123456789"
}
{
"traceId": "5342243fd424468ab9ad13d03ffcdc62",
"success": true,
"status": 10000,
"msg": "OK",
"code": "SUCCESS"
}
activity_catalog这个Hash结构,用来配置活动信息(活动id、名称、开始/结束时间、活动准入规则等);全部活动共用这一个结构
activity_items:$活动id这个Hash结构,用来配置指定活动的商品信息(活动库存、限购量、活动价等);每一个活动都有一个这样的结构
buyer_hold:$活动id:$商品id,这个Hash结构,用来记录买家在某个活动拍下活动商品的数量
item_salse:$活动id,这个Hash结构,用来记录活动商品销量
stock_reduce_flow$活动id,这个Hash结构,称为库存扣减流水表。用来记录「哪个活动(activityId)哪个买家(buyerId)在何时(orderTime)下了哪个订单(orderId)拍下哪个商品(itemId)多少件(quantity)」这一库存扣减流水
buyer_hold:$活动id:$商品id,可以知道用户已拍下多少件,就能做到控制用户累计只能买N件;
通过 item_salse:$活动id 能知道商品已售出多少件,再结合商品的库存限制,就有办法去控制库存避免超卖;同时也能给C端透出商品秒杀进度。
通过库存扣减流水stock_reduce_flow$活动id,在做回库存的时候,就能依据这一流水,知道去回哪个商品的库存、以及回哪个买家的已拍下数量。
hincrby buyer_hold:$活动id:$商品id $买家id $抢购数量
hincrby item_sales:$活动id $商品id $抢购数量
hset stock_reduce_flow:$活动id $订单id $json化库存扣减流水
hincrby buyer_hold:$活动id:$商品id $买家id -1*$抢购数量
hincrby item_sales:$活动id $商品id -1*$抢购数量
hdel stock_reduce_flow:$活动id $订单id
https://blog.csdn.net/caiguoxiong0101/article/details/104730973