• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

재고 데이터를 Redis에서 관리했을 때 분산락의 필요성

23.06.16 23:07 작성 조회수 611

0

  1. 현재 강의에서 재고 데이터를 MySQL에서 관리하고 있는데, 만약에 다중 서버 환경이라고 가정하고, 재고 데이터를 Redis에서 관리한다했을 때에도 분산락이 필요한건가요?

  2. 제가 redisson을 이용해서 재고 감소 시키는 로직과 redisson을 이용하지 않고 재고 감소 시키는 로직을 구현했는데, 제가 예상한 바로는 redisson을 이용하지 않고 재고 감소 시킬 때에는 데이터 정합성이 맞지 않고, redisson을 이용해서 재고 감소시키는 로직에선느 데이터 정합성이 맞을 것이다라고 생각했는데, 결과는 둘다 동일하게 데이터 정합성이 맞더라구요. 왜 그런걸까요?

1번째 코드는 구현코드고, 2번째 코드는 테스트 코드입니다.

 

@Repository
public class InventoryRepository {

    private final RedisStringsRepository redisStringsRepository;

    private final RedissonClient redissonClient;

    private int waitTimeForAcquiringLock  = 1;

    private int leaseTimeForLock  = 1;

    @Autowired
    public InventoryCommandRepository(
        RedisStringsRepository redisStringsRepository,
        RedissonClient redissonClient
    ) {
        this.redisStringsRepository = redisStringsRepository;
        this.redissonClient = redissonClient;
    }

    public void set(String key, int amount) {
        redisStringsRepository.set(key, String.valueOf(amount));
    }

    public void delete(String key) {
        redisStringsRepository.delete(key);
    }

    // lock 없이 재고 로직 감소
    public void decreaseByAmountWithoutLock(String key, int amount) {
        redisStringsRepository.decreaseByAmount(key, Long.valueOf(amount));
    }

    // lock 하고 재고 로직 감소
    public void decreaseByAmount(String key, int amount) {

        RLock rlock = redissonClient.getLock(key+"lock");

        try {

            boolean available = rlock.tryLock(waitTimeForAcquiringLock, leaseTimeForLock, TimeUnit.SECONDS);

            if (!available) {
                System.out.println("lock 획득 실패 "); 
                return;
            }

            redisStringsRepository.decreaseByAmount(key, Long.valueOf(amount));

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
            if (rlock != null && rlock.isLocked()) {
                rlock.unlock();
            }
        }
    }
}

 

@DisplayName("InventoryRepository")
@SpringBootTest
public class InventoryRepositoryTest {

    @Autowired
    private InventoryRepository inventoryRepository;

    @Autowired
    private RedisStringsRepository redisStringsRepository;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    String key = "testKey";

    int initialAmount = 100;

    @BeforeEach
    public void setUp() {
        redisStringsRepository.set(key, String.valueOf(initialAmount));
    }

    @AfterEach
    void teardown() {
        redisStringsRepository.delete(key);
    }

    @Nested
    @DisplayName("decreaseByAmountWithoutLock")
    class Describe_decreaseByAmountWithoutLock {

        @Nested
        @DisplayName("with 1 thread")
        class Context_With_Single_Thread {

            @Test
            @DisplayName("decreases inventory by amount")
            void It_Decreases_Inventory_By_Amount() throws InterruptedException {
                int decreaseAmount = 1;

                inventoryRepository.decreaseByAmountWithoutLock(key, decreaseAmount);

                String value = redisStringsRepository.get(key);

                int expectedAmount = initialAmount - decreaseAmount;

                assertEquals(expectedAmount, Integer.valueOf(value));
            }
        }

        @Nested
        @DisplayName("with multi thread")
        class Context_With_Multi_Thread {

            @Test
            @DisplayName("does not decrease inventory by amount")
            void It_Does_Not_Decrease_Inventory_By_Amount() throws InterruptedException {
                int threadCount = 100;

                int decreaseAmount = 1;

                ExecutorService executorService = Executors.newFixedThreadPool(32);

                CountDownLatch latch = new CountDownLatch(threadCount);

                for (int i = 0; i < threadCount; i++) {
                    executorService.submit(() -> {
                        try {
                            // Perform the test
                            inventoryRepository.decreaseByAmountWithoutLock(key, decreaseAmount);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        } finally {
                            latch.countDown();
                        }
                    });
                }

                latch.await();

                String value = redisStringsRepository.get(key);

                assertNotEquals(0, Integer.valueOf(value)); // 테스트 통과 안함.
            }
        }
    }

    @Nested
    @DisplayName("decreaseByAmount")
    class Describe_decreaseByAmount {

        @Nested
        @DisplayName("with 1 thread")
        class Context_With_Single_Thread {

            @Test
            @DisplayName("decreases inventory by amount")
            void It_Decreases_Inventory_By_Amount() throws InterruptedException {
                int decreaseAmount = 1;

                inventoryRepository.decreaseByAmount(key, decreaseAmount);

                String value = redisStringsRepository.get(key);

                int expectedAmount = initialAmount - decreaseAmount;

                assertEquals(expectedAmount, Integer.valueOf(value));
            }
        }

        @Nested
        @DisplayName("with multi thread")
        class Context_With_Multi_Thread {

            @Test
            @DisplayName("decrease inventory by amount")
            void It_Does_Not_Decrease_Inventory_By_Amount() throws InterruptedException {
                int threadCount = 100;

                int decreaseAmount = 1;

                ExecutorService executorService = Executors.newFixedThreadPool(32);

                CountDownLatch latch = new CountDownLatch(threadCount);

                for (int i = 0; i < threadCount; i++) {
                    executorService.submit(() -> {
                        try {
                            // Perform the test
                            inventoryRepository.decreaseByAmount(key, decreaseAmount);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        } finally {
                            latch.countDown(); // Latch의 숫자가 1개씩 감소
                        }
                    });
                }

                latch.await(); // Latch의 숫자가 0이 될 때까지 기다리는 코드

                String value = redisStringsRepository.get(key);

                assertEquals(0, Integer.valueOf(value));
            }
        }
    }
}

 

 

답변 1

답변을 작성해보세요.

0

linky dev 님 안녕하세요!

1. 재고데이터를 redis 에서만 관리한다면 비즈니스 로직에 따라 다르겠지만 분산락이 필요없을수도 있습니다.
다만, redis 는 휘발성 데이터를 저장할 때 적합한 데이터베이스이므로 redis 에서만 재고를 관리하는것은 추천드리지 않습니다.

2.현재 첨부해주신 소스는 "재고의 수량" 만을 redis 로 관리하고 있는것으로 보입니다.
redis 는 싱글스레드로 작업을 수행하기때문에 동시성이슈가 발생하지 않습니다.

감사합니다.

백린이님의 프로필

백린이

질문자

2023.06.24

  1. 넵, MySQL을 백업용으로 사용하고 있긴 합니다. 단순히 재고데이터를 증가/감소하는 명령어 1개만 있을 때에는 분산락이 필요없을 것 같고, 예를 들어, 여러개의 Redis 명령어(읽기/쓰기)들이 원자성 있게 관리되어야 하는 경우에는 분산락이 필요할 것 같은데, 맞을까요?

  2. 위 코드가 단순히 Redis에서 재고량 감소하는 로직만 있어서, 다중 서버에서 이 명령어를 redis에 요청해도, Redis는 싱글 스레드고 동작하므로, 동시성 이슈가 발생하지 않아, 데이터 정합성이 맞는 걸로 이해하며 될까요?

  3. 분산락의 key와 Redis 저장할 때 key 다른건가요?