Skip to content

仅使用mysql实现库存秒杀并保证数据最终一致性

Notifications You must be signed in to change notification settings

huangtaojava/seckill-mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

库存秒杀demo

1.项目分为order下单服务,stock库存服务,分布式部署,服务之间通过dubbo调用
2.无redis、mq依赖,仅使用mysql数据库,单线程队列批量扣减库存
3.通过定时任务回查和db唯一索引兜底,保证了库存扣减的最终一致性

sql脚本:

-- auto-generated definition
create table shop_order
(
    id          bigint auto_increment
        primary key,
    order_id    bigint                             not null comment '订单ID',
    user_id     bigint                             not null comment '用户ID',
    success     int                                not null,
    create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    constraint shop_order_order_id_uindex
        unique (order_id)
) comment '订单表';

create index shop_order_user_id_index
    on shop_order (user_id);

-- auto-generated definition
create table shop_stock
(
    id          bigint auto_increment
        primary key,
    sku_id      bigint                             not null comment 'SKU ID',
    name        varchar(255)                       not null comment '商品名称',
    count       bigint   default 0                 not null comment '库存数量',
    create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间'
) comment '商品库存表';

-- auto-generated definition
create table shop_stock_record
(
    id          bigint auto_increment
        primary key,
    order_id    bigint                             not null comment '订单id',
    user_id     bigint                             not null comment '用户ID',
    sku_id      bigint                             not null comment 'SKU ID',
    count       int                                not null comment '库存变更数量',
    status      tinyint  default 0                 not null comment '状态:0-初始化,1-已成功创建订单,2-库存已回滚',
    create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    constraint shop_stock_record_order_id_sku_id_uindex
        unique (order_id, sku_id)
) comment '库存操作记录表';

create index shop_stock_record_create_time_status_index
    on shop_stock_record (create_time, status);

create index shop_stock_record_user_id_index
    on shop_stock_record (user_id);

About

仅使用mysql实现库存秒杀并保证数据最终一致性

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages