Redis implements distributed consistent transactions

       We often encounter a situation that the method will call the method of the system to do some operations, and then call the method of the external system to do some operations. We need to roll back or perform compensation operations, here we can use redis.

      

        //Update the database, remember redis before the operation
        String seckillCheckedListKey = "secKillCheckedList";
        String valKey = applyWare.getId() + "_" + DateUtil.formatDateTime(new Date());
        this.redisClient.sadd(seckillCheckedListKey, valKey);
        boolean success = checkFlowOperate.checkApply(checkApplyParam);  ①
        LogTypeEnum.ROUTINE_CHECK_RECOMMEND.warn("checkupdate applyWareId={} db success={}", applyWare.getId(), success);
        if (!success) {
            result.setResultCode("Failed to approve the operation, please try again later!");
            return result;
        }
        // reindex, create promotion
        LogTypeEnum.ROUTINE_CHECK_RECOMMEND.warn("checkupdate applyWareId={} solr begin", applyWare.getId());
        BaseParam baseParam = checkFlowOperate.indexMQOpt (OperateType.ADD, checkApplyParam, pageView.getRoleType ()); 
        LogTypeEnum.ROUTINE_CHECK_RECOMMEND.warn("checkupdate applyWareId={} solr finish", applyWare.getId());
        
        redisClient.srem(seckillCheckedListKey, valKey);//The operation successfully deletes the audit ID in redis

    Here ① is the local operation on the database, ② is the operation of brushing solr and pushing MQ, it is possible that ① succeeds and ② fails, so this kind of situation needs to be recorded and then processed, so it will be stored in the file at the beginning. In redis, then after executing ①②, remove the data in redis, so that you can use a timer to run, and then get the problematic data from redis and execute it in a loop, refresh solr and send MQ, and then Removed from redis.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326505197&siteId=291194637