C.W.K.
Stream
Lesson 02 of 07 · published

Horizontal Scaling

~12 min · production, scaling, redis

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Stateless HTTP 는 서버 추가로 scale; WebSocket 은 안

HTTP API 는 동일 서버를 round-robin LB 뒤에 추가해서 scale; 모든 request 가 자기 handler 안에 완전히 살아. WebSocket connection 은 persistent — 서버 추가가 새 connection 용량 추가하지만 다른 서버의 connection 사이 message bridge 안 함. Cross-server fan-out 위해 message bus 필요.

Redis Pub/Sub 이 표준 답

Track 4 가 도입. Production 에선 모든 WebSocket 서버가 outbound message Redis 에 publish + 받으려 subscribe; 각 서버에서 local fan-out. 서버 자유 추가; 용량 linearly scale, Redis 자체가 bottleneck 될 때까지 (매우 큰 scale; 그땐 Redis Cluster 또는 Kafka).

Socket.IO 가 adapter 동봉

Socket.IO 쓰면 scaling 한 줄: client_manager=socketio.AsyncRedisManager(...). 모든 sio.emit() 이 자동으로 모든 서버 사이 bridge. pub/sub 코드 0 줄.

Code

Topology·text
  +----------+   +----------+   +----------+
  | Server 1 |   | Server 2 |   | Server 3 |
  | Users A,B|   | Users C,D|   | Users E,F|
  +----+-----+   +----+-----+   +----+-----+
       |              |              |
       +-------+------+-------+------+
               |    Redis Pub/Sub
               |  +-----------+
               +->| redis     |
                  +-----------+

  User A sends "hi" to room R
  Server 1 publishes "ws:room:R" -> Redis
  Redis fans out to all three servers
  Each server broadcasts locally to room R members
  -> A,B,C,D,E,F all receive "hi" within ms
Socket.IO Redis adapter·python
import socketio

sio = socketio.AsyncServer(
    async_mode='asgi',
    client_manager=socketio.AsyncRedisManager('redis://redis:6379'),
)

# That's the entire change. sio.emit() now spans all servers.

External links

Exercise

한 Redis 뒤에 두 Python Socket.IO 서버 띄워. 각자에 클라 열어. 서버 1 의 클라에서 chat:message 보내; Redis adapter 통해 서버 2 의 클라가 받는지 확인. Redis 멈춰 cross-server delivery 멈추는지 확인; restart 후 재개되는지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.