
Notification System Design알림 시스템 설계
알림 시스템은 이메일, SMS, 푸시 알림을 통해 다수 사용자에게 메시지를 전달하는 대규모 분산 시스템이다.
요구사항
기능 요구사항:
- 이메일, SMS, 모바일 푸시, 웹 푸시 지원
- 소프트/하드 옵트인 지원
- 중요 알림은 사용자 설정 무시
비기능 요구사항:
- 일 1억 건 이메일, 1천만 건 SMS, 5천만 건 푸시
- 소프트 실시간 (몇 초 내 전달)
- 최소 1회 전달 보장 (중복 처리 가능)시스템 아키텍처
알림 서비스
↓ 유저 설정 확인
메시지 큐 (Kafka)
┌───────────────────────────┐
│ Email Queue │ SMS Queue │
│ Push Queue │ WebPush Q │
└───────────────────────────┘
↓ 워커 처리
서드파티 서비스
┌──────────────────────────────────────┐
│ SendGrid │ Twilio │ FCM │ APNs │ Web │
└──────────────────────────────────────┘
↓
알림 로그 DB (전달 상태 추적)구현 예시
python
from dataclasses import dataclass
from enum import Enum
import json
import kafka
class NotificationType(Enum):
EMAIL = "email"
SMS = "sms"
PUSH = "push"
@dataclass
class Notification:
user_id: str
type: NotificationType
title: str
body: str
metadata: dict
class NotificationService:
def __init__(self):
self.producer = kafka.KafkaProducer(
bootstrap_servers=['kafka:9092'],
value_serializer=lambda v: json.dumps(v).encode())
def send(self, notification: Notification):
# 사용자 알림 설정 확인
prefs = self.get_user_prefs(notification.user_id)
if not prefs.get(notification.type.value, True):
return # 사용자가 비활성화한 채널
# 메시지 큐에 전송
topic = f"notification-{notification.type.value}"
self.producer.send(topic, {
"user_id": notification.user_id,
"title": notification.title,
"body": notification.body,
"metadata": notification.metadata,
"idempotency_key": f"{notification.user_id}-{hash(notification.title)}"
})중복 알림 방지
python
# idempotency_key로 중복 처리
def process_notification(msg):
key = msg['idempotency_key']
if redis.exists(f"sent:{key}"):
return # 이미 처리됨
redis.setex(f"sent:{key}", 86400, "1") # 24시간 TTL
# 실제 전송...