首页  

接口幂等几种实现     所属分类 architecture 浏览量 774
幂等 idempotent

form表单 重复提交
接口超时 重试机制 
mq消费 重复消息

接口幂等性是指用户对于同一操作发起的一次请求或者多次请求的结果是一致的,不会因为多次点击而产生副作用。

insert update 

防重 和 幂等
防重避免产生重复数据,对接口返回没有太多要求
幂等除了避免产生重复数据之外,还要求每次请求都返回一样的结果


各种实现方案

insert前先select ,不存在 才insert

悲观锁
select * from account where id=xxx for update;

乐观锁
update account set amount=amount+100,version=version+1 
where id=xxx and version=x;

唯一索引

防重表
防重表和业务表操作需要在一个事务里



状态机
status从2变到3
update account set status=3 where id=xxx and status=2;

分布式锁
防重表 和 唯一索引 ,相当于基于数据库的分布式锁
可基于redis 

jedis
public String set(final String key, final String value, final SetParams params)

params 
NX|XX
NX  Only set the key if it does not already exist. 
XX -- Only set the key if it already exist. 
EX|PX, expire time units: EX = seconds; PX = milliseconds
   

   
token


如何防止表单重复提交

上一篇     下一篇
字符串编辑距离 Levenstein edit distance

最简单的RPC框架实例

aerospike lua 构建有序列表

java NIO 服务端例子2

Spring容器初始化过程

中国诗词巅峰40首