docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거

Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
This commit is contained in:
Antigravity Agent
2026-07-05 00:33:48 +09:00
parent 1cfd3bbb56
commit 9148c358d0
6455 changed files with 1 additions and 86875 deletions
@@ -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.
## 🔗 관련 문서