refactor: 비관적 락 적용 및 좋아요, 북마크 동시성 문제 해결 #373
Merged
Conversation
Contributor
|
동시성 문제 상황을 꼼꼼하게 재현하고, 다양한 접근 방식을 실험한 뒤 최적의 방법을 적용하셨네요👍 |
sangu1026
approved these changes
Sep 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 관련 이슈
피드 좋아요, 북마크 갱신 누락 동시성 문제 #372
🔥 겪은 문제
기존 코드는
(user_id, feed_id)에 대한UNIQUE KEY제약 조건만으로 동시성을 일부 제어하고 있었습니다.이 방식은 단일 사용자의 중복 요청은 막을 수 있지만, 다수의 사용자가 동시에 요청할 경우 발생하는 갱신 손실 문제는 방지하지 못했습니다.
실제 멀티 스레드 테스트 환경(
100 threads)에서 문제를 재현한 결과,FeedLike레코드는 100개가 정상적으로 생성되었음에도 불구하고Feed엔티티의likeCount는 10으로 기록되어, 90%의 카운트 데이터가 유실되는 데이터 불일치 현상을 확인했습니다.👨🔬 다양한 동시성 제어 방식 검증
가장 적합한 해결책을 찾기 위해, 테스트 코드를 기반으로 다음과 같은 동시성 제어 기법들을 순차적으로 실험하고 장단점을 분석했습니다.
SERIALIZABLE)synchronized⚒️ 작업 내용
테스트 결과, 비관적 락이 가장 안정적이고 효율적인 해결책임을 확인했습니다.
FeedRepository에findByIdForUpdate메소드를 추가하여, 접근 시 배타적 락을 설정하도록 구현했습니다.비관적 락 적용 후 멀티 스레드 테스트 환경(
100 threads)에서 문제를 재현한 결과 100개가 정상적으로 생성된 후likeCount역시 100개를 기록하며 갱신 누락 문제를 해결했습니다.📎 커밋 범위 링크