栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > 缓存机制 > Redis缓存

使用lua+redis解决发多张券的并发问题

Redis缓存 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用lua+redis解决发多张券的并发问题

前言

公司有一个发券的接口有并发安全问题,下面列出这个问题和解决这个问题的方式。

业务描述

这个接口的作用是给会员发多张券码。涉及到4张主体,分别是:用户,券,券码,用户领取记录。
下面是改造前的伪代码。
主要是因为查出券码那行存在并发安全问题,多个线程拿到同几个券码。以下都是基于如何让取券码变成原子的去展开。

public boolean sendCoupons(Long userId, Long couponId) {
 // 一堆校验
 // ...
 // 查出券码
 List couponCodes = couponCodeService.findByCouponId(couponId, num);
 // batchUpdateStatus是一个被@Transactional(propagation = Propagation.REQUIRES_NEW)修饰的方法
 // 批量更新为已被领取状态
 couponCodeService.batchUpdateStatus(couponCods);
 // 发券
 // 发权益
 // 新增用户券码领取记录
}
改造过程

因为券码是多张,想用lua+redis的list结构去做弹出。为什么用这种方案是因为for update直接被否了。

这是写的lua脚本。。

local result = {}
for i=1,ARGV[1],1 do
 result[i] = redis.call("lpop", KEYS[1])
end
return table.contact(result , "|")

这是写的执行lua脚本的client。。其实主要的解决方法就是在redis的list里rpush(存),lpop(取)取数据

@Slf4j
@Component
public class CouponCodeRedisQueueClient implements InitializingBean {

 
 public static final String POP_COUPON_CODE_LUA_PATH = "lua/pop-coupon-code.lua";
 public static final String SEPARATOR = "|";

 private static final String COUPON_CODE_KEY_PATTERN = "PROMOTION:COUPON_CODE_{0}";
 private String LUA_COUPON_CODE_script;

 private String LUA_COUPON_CODE_script_SHA;

 @Autowired
 private JedisTemplate jedisTemplate;

 @Override
 public void afterPropertiesSet() throws Exception {

  LUA_COUPON_CODE_script = Resources.toString(Resources.getResource(POP_COUPON_CODE_LUA_PATH), Charsets.UTF_8);
  if (StringUtils.isNotBlank(LUA_COUPON_CODE_script)) {

   LUA_COUPON_CODE_script_SHA = jedisTemplate.execute(jedis -> {
    return jedis.scriptLoad(LUA_COUPON_CODE_script);
   });
   log.info("redis lock script sha:{}", LUA_COUPON_CODE_script_SHA);
  }

 }

 
 public List popCouponCode(Long activityId, String num , int retryNum) {
  if(retryNum == 0){
   log.error("reload lua script error , try limit times ,activityId:{}", activityId);
   return Collections.emptyList();
  }
  List keys = Lists.newArrayList();
  String key = buildKey(String.valueOf(activityId));
  keys.add(key);
  List args = Lists.newArrayList();
  args.add(num);

  try {
   Object result = jedisTemplate.execute(jedis -> {
    if (StringUtils.isNotBlank(LUA_COUPON_CODE_script_SHA)) {
     return jedis.evalsha(LUA_COUPON_CODE_script_SHA, keys, args);
    } else {
     return jedis.eval(LUA_COUPON_CODE_script, keys, args);
    }
   });
   log.info("pop coupon code by lua script.result:{}", result);
   if (Objects.isNull(result)) {
    return Collections.emptyList();
   }
   return Splitter.on(SEPARATOR).splitToList(result.toString());
  } catch (JedisNoscriptException jnse) {
   log.error("no lua lock script found.try to reload it", jnse);
   reloadLuascript();
   //加载后重新执行
   popCouponCode(activityId, num, --retryNum);
  } catch (Exception e) {
   log.error("failed to get a redis lock.key:{}", key, e);
  }
  return Collections.emptyList();
 }

 
 public void reloadLuascript() {
  synchronized (CouponCodeRedisQueueClient.class) {
   try {
    afterPropertiesSet();
   } catch (Exception e) {
    log.error("failed to reload redis lock lua script.retry load it.");
    reloadLuascript();
   }
  }
 }

 
 public String buildKey(String activityId) {
  return MessageFormat.format(COUPON_CODE_KEY_PATTERN, activityId);
 }

}

当然这种操作需要去提前把所有券的券码丢到redis里去,这里我们也碰到了一些问题(券码量比较大的情况下)。比如开始直接粗暴的用@PostConstruct去放入redis,导致项目启动需要很久很久。。这里就不展开了,说一下我们尝试的几种方法

  • @PostConstruct注解
  • CommandLineRunner接口
  • redis的pipeline技术
  • 先保证每个卡券有一定量的券码在redis,再用定时任务定时(根据业务量)去补

到此这篇关于使用lua+redis解决发多张券的并发问题的文章就介绍到这了,更多相关redis多张券的并发内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/173259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号