refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게 [공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류. 문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인). - Topic_Programming → Domain_Programming (내부 구조 보존) - Topic_Graphic → Domain_Design - Topic_Business → Domain_Product - Topic_General → Domain_General - _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning), Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing) - 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서) - 빈 폴더 정리 (memory/procedures) - 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
---
|
||||
id: backend-websocket-scaling
|
||||
title: WebSocket Scaling — Pub/Sub / Sticky / Heartbeat
|
||||
category: Coding
|
||||
status: draft
|
||||
source_trust_level: B
|
||||
verification_status: conceptual
|
||||
created_at: 2026-05-09
|
||||
updated_at: 2026-05-09
|
||||
tags: [backend, websocket, scaling, pubsub, vibe-coding]
|
||||
tech_stack: { language: "TS / Node / Redis", applicable_to: ["Backend"] }
|
||||
applied_in: []
|
||||
aliases: [WS scaling, Redis pub/sub, sticky session, socket.io adapter]
|
||||
---
|
||||
|
||||
# WebSocket Scaling
|
||||
|
||||
> 1대 서버 = 수만 connections OK. **N 대 서버 분산 시** 메시지 중계가 핵심. Redis pub/sub, NATS, Kafka 사용. **Sticky session + heartbeat + reconnect with backoff**.
|
||||
|
||||
## 📖 핵심 개념
|
||||
- 1 connection = 한 process 메모리. 1대 ~50K 까지 보통.
|
||||
- Multi-node 시 client A → server 1, client B → server 2 — 어떻게 broadcast?
|
||||
- Heartbeat: idle conn 정리, NAT timeout 방지.
|
||||
|
||||
## 💻 코드 패턴
|
||||
|
||||
### 단일 서버 — ws
|
||||
```ts
|
||||
import { WebSocketServer } from 'ws';
|
||||
|
||||
const wss = new WebSocketServer({ port: 8080 });
|
||||
const rooms = new Map<string, Set<WebSocket>>();
|
||||
|
||||
wss.on('connection', (ws, req) => {
|
||||
const room = new URL(req.url!, 'http://x').searchParams.get('room')!;
|
||||
if (!rooms.has(room)) rooms.set(room, new Set());
|
||||
rooms.get(room)!.add(ws);
|
||||
|
||||
ws.on('message', (data) => {
|
||||
for (const peer of rooms.get(room)!) {
|
||||
if (peer !== ws && peer.readyState === ws.OPEN) peer.send(data);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => rooms.get(room)?.delete(ws));
|
||||
});
|
||||
```
|
||||
|
||||
### Multi-node — Redis pub/sub
|
||||
```ts
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const pub = new Redis(url);
|
||||
const sub = new Redis(url);
|
||||
|
||||
sub.subscribe('room:42', () => {});
|
||||
sub.on('message', (channel, msg) => {
|
||||
const room = channel.split(':')[1];
|
||||
for (const peer of localRooms.get(room) ?? []) peer.send(msg);
|
||||
});
|
||||
|
||||
// 메시지 도착하면
|
||||
pub.publish(`room:${room}`, JSON.stringify(message));
|
||||
```
|
||||
|
||||
### Heartbeat (양방향)
|
||||
```ts
|
||||
function heartbeat(ws: WebSocket) {
|
||||
let alive = true;
|
||||
ws.on('pong', () => { alive = true; });
|
||||
|
||||
const t = setInterval(() => {
|
||||
if (!alive) { ws.terminate(); return; }
|
||||
alive = false;
|
||||
ws.ping();
|
||||
}, 30_000);
|
||||
|
||||
ws.on('close', () => clearInterval(t));
|
||||
}
|
||||
```
|
||||
|
||||
### Reconnect (client) with backoff
|
||||
```ts
|
||||
class ReconnectingWS {
|
||||
private retries = 0;
|
||||
connect() {
|
||||
this.ws = new WebSocket(this.url);
|
||||
this.ws.onopen = () => { this.retries = 0; };
|
||||
this.ws.onclose = () => {
|
||||
const delay = Math.min(1000 * 2 ** this.retries, 30_000) + Math.random() * 1000;
|
||||
this.retries++;
|
||||
setTimeout(() => this.connect(), delay);
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Backpressure
|
||||
```ts
|
||||
// ws send 가 buffer 한계 초과 시
|
||||
if (ws.bufferedAmount > 1_000_000) {
|
||||
// drop or disconnect — 느린 client 가 메모리 고갈
|
||||
ws.close(1009, 'too slow');
|
||||
}
|
||||
```
|
||||
|
||||
### LB sticky (cookie or IP hash)
|
||||
```nginx
|
||||
upstream ws_backend {
|
||||
ip_hash; # 간단
|
||||
server ws1:8080;
|
||||
server ws2:8080;
|
||||
}
|
||||
server {
|
||||
location /ws {
|
||||
proxy_pass http://ws_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_read_timeout 3600s;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Auth (during handshake)
|
||||
```ts
|
||||
wss.on('connection', (ws, req) => {
|
||||
const token = new URL(req.url!, 'http://x').searchParams.get('token');
|
||||
const user = verifyJwt(token);
|
||||
if (!user) return ws.close(4401, 'unauth');
|
||||
ws.user = user;
|
||||
});
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준
|
||||
| 규모 | 솔루션 |
|
||||
|---|---|
|
||||
| <10K conn | 단일 Node + ws |
|
||||
| <100K | 다중 Node + Redis pub/sub |
|
||||
| 100K+ | NATS / Kafka / 전용 service (Centrifugo, Soketi) |
|
||||
| Pub/sub + 메시지 영속 | Kafka / Redis Streams |
|
||||
| 게임 (low latency) | UDP / WebRTC |
|
||||
| Chat / 알림 | WebSocket / SSE |
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Heartbeat 없음**: NAT timeout (60s+) 후 dead conn 남음.
|
||||
- **Reconnect 즉시 무한**: 서버 다운 시 thundering herd.
|
||||
- **Auth 만 메시지 첫 번째로**: 무인증 conn 점유. handshake 에서.
|
||||
- **Send back-pressure 무시**: 메모리 폭발.
|
||||
- **Single-node assumption**: 두 서버 띄우면 broadcast 안 됨.
|
||||
- **메시지에 PII 그대로**: TLS 필수 (wss).
|
||||
- **Reconnect 시 missed message 무시**: server 측 message id 큐 + replay.
|
||||
|
||||
## 🤖 LLM 활용 힌트
|
||||
- Heartbeat (30s) + reconnect (exponential + jitter) + sticky (ip_hash).
|
||||
- N 노드 = Redis pub/sub.
|
||||
|
||||
## 🔗 관련 문서
|
||||
Reference in New Issue
Block a user