Files
2nd/10_Wiki/Topics/Architecture/Publish-Subscribe Model.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

182 lines
5.2 KiB
Markdown

---
id: wiki-2026-0508-publish-subscribe-model
title: Publish-Subscribe Model
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Pub-Sub, Pub/Sub, Topic-Based Messaging]
duplicate_of: none
source_trust_level: A
confidence_score: 0.95
verification_status: applied
tags: [messaging, eda, distributed-systems, pattern]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: python
framework: kafka-redis-nats
---
# Publish-Subscribe Model
## 매 한 줄
> **"매 publisher 매 subscribers 매 모름 — 매 topic / channel 매 통해 매 decoupled broadcast"**. 1987 ISIS Toolkit 매 origin, 매 today Kafka / Redis Pub/Sub / NATS / Google Pub/Sub / AWS SNS 매 ubiquitous backbone.
## 매 핵심
### 매 model variants
- **Topic-based**: 매 logical channel name (e.g. `orders.created`).
- **Content-based**: 매 message attributes 매 filter — 매 RabbitMQ headers exchange.
- **Type-based**: 매 message class hierarchy.
- **Hybrid**: 매 topic + filter (NATS subject wildcards `orders.*.created`).
### 매 delivery semantics
- **At-most-once**: fire-and-forget (Redis Pub/Sub).
- **At-least-once**: ack required (Kafka, SQS).
- **Exactly-once**: 매 idempotent + transactional (Kafka EOS, Pulsar).
### 매 응용
1. Event-driven microservices (orders → fulfillment, billing, notifications).
2. Real-time dashboards (Grafana Live, websocket fanout).
3. CDC (Debezium → Kafka → consumers).
4. IoT telemetry (MQTT — pub-sub variant).
## 💻 패턴
### In-process pub-sub (Python)
```python
from collections import defaultdict
from typing import Callable
class Bus:
def __init__(self):
self.subs: dict[str, list[Callable]] = defaultdict(list)
def subscribe(self, topic, fn): self.subs[topic].append(fn)
def publish(self, topic, msg):
for fn in self.subs[topic]:
fn(msg)
bus = Bus()
bus.subscribe("user.created", lambda u: print(f"welcome {u}"))
bus.publish("user.created", "alice")
```
### Redis Pub/Sub
```python
import redis, json
r = redis.Redis()
# Publisher
r.publish("orders.created", json.dumps({"id": 42, "total": 99}))
# Subscriber
ps = r.pubsub()
ps.subscribe("orders.*")
for msg in ps.listen():
if msg["type"] == "pmessage":
handle(json.loads(msg["data"]))
```
### Kafka producer/consumer (Python)
```python
from confluent_kafka import Producer, Consumer
p = Producer({"bootstrap.servers": "localhost:9092"})
p.produce("orders", key="42", value=b'{"total":99}')
p.flush()
c = Consumer({
"bootstrap.servers": "localhost:9092",
"group.id": "fulfillment",
"auto.offset.reset": "earliest",
})
c.subscribe(["orders"])
while True:
msg = c.poll(1.0)
if msg and not msg.error():
process(msg.value())
c.commit(msg)
```
### NATS with subject wildcards
```python
import nats, asyncio
async def main():
nc = await nats.connect("nats://localhost:4222")
async def cb(msg): print(msg.subject, msg.data)
await nc.subscribe("orders.*.created", cb=cb)
await nc.publish("orders.US.created", b'{"id":42}')
await asyncio.sleep(1)
asyncio.run(main())
```
### Google Cloud Pub/Sub
```python
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic = publisher.topic_path("proj", "orders")
publisher.publish(topic, b'{"id":42}', region="us-east1")
sub = pubsub_v1.SubscriberClient()
sub_path = sub.subscription_path("proj", "fulfillment-sub")
def cb(msg): handle(msg.data); msg.ack()
sub.subscribe(sub_path, callback=cb).result()
```
### AsyncAPI schema (governance, 2026)
```yaml
asyncapi: 3.0.0
channels:
ordersCreated:
address: orders.created
messages:
OrderCreated:
payload:
type: object
properties:
id: {type: integer}
total: {type: number}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 매 in-process | Bus / EventEmitter |
| 매 ephemeral, low-latency | Redis Pub/Sub / NATS |
| 매 durable, replay 필요 | Kafka / Pulsar |
| 매 cloud-native, managed | Google Pub/Sub / AWS SNS+SQS |
| 매 IoT / mobile | MQTT |
| 매 fan-in-out, transform | Kafka Streams / Pulsar Functions |
**기본값**: 매 production EDA — Kafka. 매 simple/ephemeral — NATS.
## 🔗 Graph
- 부모: [[Event Driven Architecture]]
- 변형: [[Observer Pattern]] · [[Event Bus]]
- 응용: [[Event Sourcing]] · [[CQRS]] · [[CDC]]
- Adjacent: [[Kafka]] · [[NATS]] · [[Redis]] · [[MQTT]]
## 🤖 LLM 활용
**언제**: 매 schema design (AsyncAPI generation), 매 consumer code scaffolding, 매 dead-letter analysis.
**언제 X**: 매 broker selection 매 trust 의 X — workload metrics 직접 측정.
## ❌ 안티패턴
- **Sync pub-sub**: 매 publisher block on slow subscriber — 매 async / queue 사용.
- **No schema**: 매 consumer breakage — Avro/Protobuf + registry 필수.
- **Topic explosion**: 매 user-per-topic — 매 millions of topics, broker 죽음. 매 partition / filter 사용.
- **Duplicate semantics**: 매 at-least-once 인데 매 idempotency 없음 — duplicate processing.
## 🧪 검증 / 중복
- Verified (Eugster et al. — "The Many Faces of Publish/Subscribe" 2003; Kafka docs).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full pub-sub pattern entry |