@startuml analytics-이벤트생성구독 !theme mono title Analytics Service - EventCreated 이벤트 구독 처리 내부 시퀀스\n(Kafka Event 구독) participant "Kafka Consumer" as Consumer participant "EventCreatedListener" as Listener participant "AnalyticsService" as Service participant "AnalyticsRepository" as Repository participant "CacheService" as Cache participant "Redis" as Redis database "Analytics DB" as DB note over Consumer **Kafka Consumer 설정** - Topic: EventCreated - Consumer Group: analytics-service - Partition Key: eventId - At-Least-Once Delivery 보장 end note Kafka -> Consumer: EventCreated 이벤트 수신\n{\n eventId: "uuid",\n storeId: "uuid",\n title: "이벤트 제목",\n objective: "신규 고객 유치",\n createdAt: "2025-10-22T10:00:00Z"\n} activate Consumer Consumer -> Listener: onEventCreated(event) activate Listener note right of Listener **멱등성 체크** - Redis Set에 이벤트 ID 존재 여부 확인 - 중복 처리 방지 end note Listener -> Redis: SISMEMBER processed_events {eventId} activate Redis alt 이벤트 미처리 (멱등성 보장) Redis --> Listener: false (미처리) deactivate Redis Listener -> Service: initializeEventStats(event) activate Service note right of Service **이벤트 통계 초기화** - 이벤트 기본 정보 저장 - 통계 초기값 설정 * 총 참여자 수: 0 * 총 노출 수: 0 * 예상 ROI: 계산 전 * 매출 증가율: 0% end note Service -> Repository: save(eventStatsEntity) activate Repository Repository -> DB: 이벤트 통계 초기화\n(이벤트ID, 매장ID, 제목, 목적,\n참여자수/노출수/ROI/매출증가율을\n0으로 초기화하여 저장) activate DB DB --> Repository: 1 row inserted deactivate DB Repository --> Service: EventStatsEntity deactivate Repository note right of Service **초기화 완료** - 이벤트 통계 DB 생성 - 향후 ParticipantRegistered 이벤트 수신 시 실시간 증가 end note Service -> Cache: delete("analytics:dashboard:{eventId}") activate Cache note right of Cache **캐시 무효화** - 기존 캐시 삭제 - 다음 조회 시 최신 데이터 갱신 end note Cache -> Redis: DEL analytics:dashboard:{eventId} activate Redis Redis --> Cache: OK deactivate Redis Cache --> Service: OK deactivate Cache Service -> Redis: SADD processed_events {eventId} activate Redis note right of Redis **멱등성 처리 완료 기록** - Redis Set에 eventId 추가 - TTL 설정 (7일) end note Redis --> Service: OK deactivate Redis Service --> Listener: EventStats 초기화 완료 deactivate Service Listener -> Consumer: ACK (처리 완료) deactivate Listener else 이벤트 이미 처리됨 (중복) Redis --> Listener: true (이미 처리) deactivate Redis note right of Listener **중복 이벤트 스킵** - At-Least-Once Delivery로 인한 중복 - 멱등성 보장으로 중복 처리 방지 end note Listener -> Consumer: ACK (스킵) deactivate Listener end Consumer --> Kafka: Commit Offset deactivate Consumer note over Consumer, DB **처리 시간** - 이벤트 수신 → 초기화 완료: 약 0.2초 - DB INSERT: 0.05초 - Redis 캐시 무효화: 0.01초 - 멱등성 체크: 0.01초 end note @enduml