分布式接口幂等性实现思路

get、delete请求具有天然幂等性
主要针对insert、update 实现幂等性
key:hash(uid+url+sort(reqParam))
value: {value: uid+url+sort(reqParam), result: ''}
expire: req_overtime + replyTime

let key= hash(uid+url+sort(reqParam));
let ret = get(key);
if (ret != null && ret.value == key) {
return ret.result;
}

if (setnx(key, value) == 1) {
try {
// biz operation
// if biz throw exception, next request can retry, because of remove the key.
value.result = doBiz();
set(key, value, expire);
} catch () {
     del(key)
}
} else {
//concurrent repeat request
}

posted @ 2020-06-21 15:54  呆小田  阅读(311)  评论(0编辑  收藏  举报