Skip to content

ReturnItemUseCase에서 동시성 레이스 컨디션 및 N+1 쿼리 문제 해결 #59

@coderabbitai

Description

@coderabbitai

문제 설명

ReturnItemUseCase.kt에서 두 가지 주요 문제가 발견되었습니다:

1. 동시성 레이스 컨디션

  • 두 요청이 동시에 동일 대여(rental)를 처리할 때 재고가 중복 증가할 수 있음
  • 둘 다 returnedAt == null을 관찰하고 각각 item.availableQuantity += 1을 수행
  • 데이터 무결성 훼손 가능성

2. N+1 쿼리 문제

  • 루프마다 itemJpaRepository.findById(...)를 호출
  • 성능 저하 발생

해결 방안

1. Pessimistic Lock + Fetch Join

  • Rentals 조회 시 PESSIMISTIC_WRITE 잠금 적용
  • Item을 fetch-join으로 미리 로드
  • Repository에 findAllByUsageIdForUpdate 메서드 추가

2. 낙관적 락 추가

  • Item/Rental 엔티티에 @Version 필드 추가
  • 서로 다른 usage의 동시 Item 갱신 시 잃어버린 업데이트 방지

관련 링크

담당자

@withJihyuk

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions