Files
2nd/10_Wiki/Topics/Architecture/Publish-Subscribe Model.md
T
koriweb d8a80f6272 chore(wiki): dangling 링크 canonical 정규화 (768파일/1200건)
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해
끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은
과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업.
도구: Datacollect/scripts/link_reconcile_apply.mjs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:24:15 +09:00

5.2 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-publish-subscribe-model Publish-Subscribe Model 10_Wiki/Topics verified self
Pub-Sub
Pub/Sub
Topic-Based Messaging
none A 0.95 applied
messaging
eda
distributed-systems
pattern
2026-05-10 pending
language framework
python 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)

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

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)

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

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

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)

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

🤖 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