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

Binary Protocol

~13 min · protocol, binary, msgpack, protobuf

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

JSON 이 비싸질 때

JSON 은 verbose, parse 느리고, float 압축 못 해. 고빈도 또는 numerical data — 60fps 게임 state, 100Hz market tick, sensor stream — 엔 JSON tax 가 bandwidth 비용 + dropped frame 으로 나타나. 두 binary format 우세: MessagePack (drop-in, schema-less), Protocol Buffers (compact, schema 필수).

MessagePack: JSON 모양, 더 작고 빨라

MessagePack 이 같은 JSON-ish 구조 (object, array, number, string, bool, null) 를 ~30% 더 작은 binary frame 으로 인코드, parse 훨씬 빠름. schema 정의 안 필요. JSON 비싸지면 먼저 손 가.

Protocol Buffers: schema 기반, 가장 작음

Protobuf 가 .proto schema + 코드 generation 필요한데 60% 더 작은 payload + 가장 빠른 parse. schema 가 문서화 + 버전 관리 역할. protocol 이 안정되고 데이터 양이 setup 정당화할 때 가치.

Code

브라우저 + Python 의 MessagePack·javascript
// Browser side, with msgpack-lite or msgpackr
import { encode, decode } from 'msgpackr';

ws.binaryType = 'arraybuffer';
ws.send(encode({ x: 150.5, y: 300.2, id: 42, flags: 0b0001 }));

ws.onmessage = (e) => {
  if (e.data instanceof ArrayBuffer) {
    const data = decode(new Uint8Array(e.data));
    applyState(data);
  }
};
msgpack 서버쪽·python
import msgpack
from fastapi import WebSocket

@app.websocket('/ws/binary')
async def binary(websocket: WebSocket):
    await websocket.accept()
    while True:
        raw = await websocket.receive_bytes()
        data = msgpack.unpackb(raw)
        result = process(data)
        await websocket.send_bytes(msgpack.packb(result))
Format 비교·text
| Format     | Size (typical) | Parse speed | Schema required |
| ---------- | -------------- | ----------- | --------------- |
| JSON       | baseline       | slowest     | no              |
| MessagePack| ~30% smaller   | fast        | no              |
| Protobuf   | ~60% smaller   | fastest     | yes (.proto)    |

External links

Exercise

50-field game-state dict 을 JSON, MessagePack, Protobuf 로 serialize. 측정: byte size, encode time, decode time, JS + Python 각각. 표 만들어; 60fps multiplayer 면 어느 거 픽? 분당 한 번 notification 이면?

Progress

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

댓글 0

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

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