🧑🏻‍💻 Redisson 이란?

<aside> 💡 pub-sub방식을 활용하여 Java에서 사용되는 Redis 클라이언트

</aside>

Redisson 진행

Redisson 전체 코드

  1. Redisson를 위한 implementation을 추가

    //Redis
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    //redisson
    implementation group: 'org.redisson', name: 'redisson-spring-boot-starter', version: '3.23.1'
    
  2. Redisson을 활용한 서비스 코드 작성

    @Override
    @Transactional
    public PaymentSuccessResponse approvePaymentWithLock(String pgToken) {
        Long memberId = securityUtil.getCurrentMemberId();
        String key = REDIS_CACHE_KEY_PREFIX + memberId;
        PaymentRedisDto paymentInfo = getPaymentInfo(key);
    
        RLock lock = redissonClient.getLock(REDIS_LOCK_KEY_PREFIX + paymentInfo.productId());
    
        try {
            if (!lock.tryLock(500, 5_000, TimeUnit.MICROSECONDS)) {
                throw new RuntimeException();
            }
            Product targetProduct = productRepository.findById(paymentInfo.productId())
                .orElseThrow();
            checkOutOfStock(targetProduct);
    
            return approvePayment(pgToken, memberId);
        } catch (InterruptedException e) {
            throw new RuntimeException();
        } finally {
            if (lock != null && lock.isLocked()) {
                lock.unlock();
            }
        }
    }
    

🧪 성능테스트

결과

Untitled

🚀 Lettuce와 비교