|
| 1 | +package com.loopers.application.event.listener; |
| 2 | + |
| 3 | +import com.loopers.application.event.like.ProductLikedEvent; |
| 4 | +import com.loopers.application.event.like.ProductUnlikedEvent; |
| 5 | +import com.loopers.infrastructure.cache.CacheInvalidationService; |
| 6 | +import com.loopers.infrastructure.cache.MemberLikesCache; |
| 7 | +import com.loopers.infrastructure.cache.ProductLikeCountCache; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.retry.annotation.Backoff; |
| 11 | +import org.springframework.retry.annotation.Recover; |
| 12 | +import org.springframework.retry.annotation.Retryable; |
| 13 | +import org.springframework.scheduling.annotation.Async; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | +import org.springframework.transaction.event.TransactionPhase; |
| 16 | +import org.springframework.transaction.event.TransactionalEventListener; |
| 17 | + |
| 18 | +/** |
| 19 | + * 좋아요 집계 이벤트 리스너 |
| 20 | + * |
| 21 | + * - 좋아요/취소 이벤트를 수신하여 Redis 캐시를 비동기로 업데이트 |
| 22 | + * - eventual consistency: DB 저장 후 Redis는 비동기로 업데이트 |
| 23 | + * - Redis 실패 시에도 DB는 정상 저장됨 |
| 24 | + */ |
| 25 | +@Slf4j |
| 26 | +@Component |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class LikeAggregationEventListener { |
| 29 | + |
| 30 | + private final ProductLikeCountCache productLikeCountCache; |
| 31 | + private final MemberLikesCache memberLikesCache; |
| 32 | + private final CacheInvalidationService cacheInvalidationService; |
| 33 | + |
| 34 | + /** |
| 35 | + * 좋아요 이벤트 처리 (비동기 + 재시도) |
| 36 | + * - Redis 좋아요 카운트 증가 |
| 37 | + * - 회원 좋아요 목록 캐시 업데이트 |
| 38 | + * - 상품 캐시 무효화 |
| 39 | + * |
| 40 | + * 재시도 전략: |
| 41 | + * - 최대 3회 재시도 |
| 42 | + * - 초기 딜레이 100ms, 지수 백오프 (100ms → 200ms → 400ms) |
| 43 | + * - 일시적 Redis 네트워크 오류 대응 |
| 44 | + */ |
| 45 | + @Async |
| 46 | + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) |
| 47 | + @Retryable( |
| 48 | + maxAttempts = 3, |
| 49 | + backoff = @Backoff(delay = 100, multiplier = 2), |
| 50 | + recover = "recoverProductLiked" |
| 51 | + ) |
| 52 | + public void handleProductLiked(ProductLikedEvent event) { |
| 53 | + log.info("[LikeAggregationEventListener] 좋아요 이벤트 처리 - memberId: {}, productId: {}", |
| 54 | + event.memberId(), event.productId()); |
| 55 | + |
| 56 | + // 재시도 가능하도록 try-catch 제거 (예외를 위로 전파) |
| 57 | + productLikeCountCache.increment(event.productId()); |
| 58 | + memberLikesCache.add(event.memberId(), event.productId()); |
| 59 | + cacheInvalidationService.invalidateOnLikeChange(event.productId(), event.brandId()); |
| 60 | + |
| 61 | + log.debug("[LikeAggregationEventListener] 좋아요 집계 완료 - productId: {}", event.productId()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 좋아요 취소 이벤트 처리 (비동기 + 재시도) |
| 66 | + * - Redis 좋아요 카운트 감소 |
| 67 | + * - 회원 좋아요 목록 캐시 업데이트 |
| 68 | + * - 상품 캐시 무효화 |
| 69 | + * |
| 70 | + * 재시도 전략: |
| 71 | + * - 최대 3회 재시도 |
| 72 | + * - 초기 딜레이 100ms, 지수 백오프 (100ms → 200ms → 400ms) |
| 73 | + * - 일시적 Redis 네트워크 오류 대응 |
| 74 | + */ |
| 75 | + @Async |
| 76 | + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) |
| 77 | + @Retryable( |
| 78 | + maxAttempts = 3, |
| 79 | + backoff = @Backoff(delay = 100, multiplier = 2), |
| 80 | + recover = "recoverProductUnliked" |
| 81 | + ) |
| 82 | + public void handleProductUnliked(ProductUnlikedEvent event) { |
| 83 | + log.info("[LikeAggregationEventListener] 좋아요 취소 이벤트 처리 - memberId: {}, productId: {}", |
| 84 | + event.memberId(), event.productId()); |
| 85 | + |
| 86 | + // 재시도 가능하도록 try-catch 제거 (예외를 위로 전파) |
| 87 | + productLikeCountCache.decrement(event.productId()); |
| 88 | + memberLikesCache.remove(event.memberId(), event.productId()); |
| 89 | + cacheInvalidationService.invalidateOnLikeChange(event.productId(), event.brandId()); |
| 90 | + |
| 91 | + log.debug("[LikeAggregationEventListener] 좋아요 취소 집계 완료 - productId: {}", event.productId()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 좋아요 집계 최종 실패 시 복구 메서드 |
| 96 | + * - 3회 재시도 후에도 실패한 경우 호출됨 |
| 97 | + * - 로그 기록 및 향후 DLQ 저장 가능 |
| 98 | + */ |
| 99 | + @Recover |
| 100 | + public void recoverProductLiked(Exception ex, ProductLikedEvent event) { |
| 101 | + log.error("[LikeAggregationEventListener] 좋아요 집계 최종 실패 - memberId: {}, productId: {}, error: {}", |
| 102 | + event.memberId(), event.productId(), ex.getMessage(), ex); |
| 103 | + |
| 104 | + // TODO: Dead Letter Queue에 저장하여 나중에 재처리 |
| 105 | + // deadLetterQueueService.save(event, ex); |
| 106 | + |
| 107 | + // TODO: 알림 전송 (심각한 Redis 장애) |
| 108 | + // alertService.sendAlert("좋아요 집계 실패", event, ex); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * 좋아요 취소 집계 최종 실패 시 복구 메서드 |
| 113 | + * - 3회 재시도 후에도 실패한 경우 호출됨 |
| 114 | + * - 로그 기록 및 향후 DLQ 저장 가능 |
| 115 | + */ |
| 116 | + @Recover |
| 117 | + public void recoverProductUnliked(Exception ex, ProductUnlikedEvent event) { |
| 118 | + log.error("[LikeAggregationEventListener] 좋아요 취소 집계 최종 실패 - memberId: {}, productId: {}, error: {}", |
| 119 | + event.memberId(), event.productId(), ex.getMessage(), ex); |
| 120 | + |
| 121 | + // TODO: Dead Letter Queue에 저장하여 나중에 재처리 |
| 122 | + // deadLetterQueueService.save(event, ex); |
| 123 | + |
| 124 | + // TODO: 알림 전송 (심각한 Redis 장애) |
| 125 | + // alertService.sendAlert("좋아요 취소 집계 실패", event, ex); |
| 126 | + } |
| 127 | +} |
0 commit comments