본문으로 건너뛰기
김신건의 로그

[Concurrency] Backpressure: 흐름 제어로 시스템 보호

· 수정 · 📖 약 1분 · 440자/단어 #backpressure #flow-control #reactive #concurrency #backend
Backpressure, flow control, reactive streams, rate shaping, buffer overflow, shedding

정의

Backpressure = 생산자가 소비자 속도에 맞춰 자기 속도 조절. 분산 시스템의 cascade failure 방지.

IMPORTANT

Backpressure 의 핵심: “받는 쪽이 처리 못 함” 을 주는 쪽이 알아야 한다. 모르면 버퍼 폭발 + OOM + 시스템 다운.

문제: 비대칭 속도

flowchart LR
    Fast[Fast Producer<br/>10000 msg/s] -->|push| Buf[Unbounded Buffer]
    Buf -->|consume| Slow[Slow Consumer<br/>100 msg/s]
    Buf -.OOM.-> Crash[💥]

Producer-Consumer 에서 Producer 가 항상 빠르면 buffer 가 무한 으로. backpressure 의 본질적 시나리오.

4가지 전략

flowchart TD
    Q[Producer > Consumer]
    Q --> Buf[1. Bounded buffer + Block]
    Q --> Drop["2. Drop (lossy)"]
    Q --> Sample[3. Sampling]
    Q --> Pull["4. Pull-based (demand)"]

1. Bounded Buffer + Block

queue = BoundedQueue(maxsize=1000)

# Producer
queue.put(msg)   # 가득 차면 block 또는 timeout
  • 단순.
  • Producer 가 blocking → 상위 backpressure 자동 전파.

2. Drop (Lossy)

try:
    queue.put_nowait(msg)
except QueueFull:
    pass  # 드롭
  • metrics / log 같은 손실 OK 데이터.
  • Drop policy: oldest, newest, sample.

3. Sampling

if random.random() < 0.1:   # 10% 만 처리
    queue.put(msg)
  • 대규모 telemetry.
  • 통계적으로 유의미.

4. Pull-based (Reactive Streams)

sequenceDiagram
    autonumber
    participant P as Producer
    participant C as Consumer

    C->>P: request(10) (demand)
    P->>C: next(msg) × 10
    C->>P: request(5) (more demand)
    P->>C: next(msg) × 5

Consumer 가 요청한 만큼만 전송. Producer 가 자기 속도 자동 조절.

라이브러리: Project Reactor (Java), RxJava, ReactiveX, Akka Streams.

시스템 레벨 Backpressure

flowchart LR
    LB --> Sv1[Service 1]
    Sv1 --> Sv2[Service 2]
    Sv2 --> DB[(DB)]
    DB -.포화.-> Sv2
    Sv2 -.503.-> Sv1
    Sv1 -.429.-> LB
    LB -.queue.-> Client
레이어신호
LB429 Too Many Requests
Service503 Service Unavailable
DBconnection pool 고갈
NetworkTCP window 축소

HTTP/2, QUIC 의 flow control

flowchart LR
    Sender -->|DATA| Receiver
    Receiver -->|WINDOW_UPDATE +N| Sender
    Note[Sender 는 window 안에서만]

자세한 건 HTTP/2 의 flow control 절.

TCP 의 flow control

TCP 의 sliding window전송 레이어 backpressure. 자세한 건 tcp.

Bulkhead

flowchart TB
    Pool1["Pool A (10 threads)<br/>→ service A"]
    Pool2["Pool B (10 threads)<br/>→ service B"]
    Pool3["Pool C (10 threads)<br/>→ service C"]
    Fail["service A 다운"]
    Fail --> Pool1
    Pool1 -.포화.-> Pool1
    Note[Pool A 만 영향. B/C 는 정상]

Thread pool 을 service 별로 분리 → 한 service 다운이 전체 다운 안 되게.

Shedding (Load Shedding)

flowchart TD
    Q[현재 부하 > 한도?]
    Q -->|예| Shed["일부 요청 거절 (503)"]
    Q -->|아니오| Process[정상 처리]

모든 요청을 느리게 처리 하는 것보다 일부 거절하고 나머지는 빠르게. Google SRE 의 graceful degradation.

Adaptive Concurrency

flowchart LR
    Lat[현재 latency 측정] --> Calc[Little's Law 로<br/>적정 동시성 계산]
    Calc --> Cap[concurrency 한도 조정]
    Cap --> Limit[새 요청 제한]
  • Netflix Concurrency Limits.
  • Envoy adaptive concurrency.
  • latency 가 늘면 동시성 줄임.

흔한 함정

WARNING

  1. Unbounded queue = OOM 보장. 모든 buffer 는 bounded.
  2. Drop 정책 명확하지 않음 = 운영자가 어떤 데이터 사라지는지 모름. 메트릭 필수.
  3. Backpressure 전파 안 됨 = 한 계층만 잡고 에서 압박 지속. cascade fail.
  4. Pull-based 의 너무 큰 request(N) = 사실상 push 와 같음. 적절한 batch size.

관련 위키

이 글의 용어 (6개)
[Concurrency] Circuit Breaker: cascade failure 방어concurrency
정의 Circuit Breaker = 전기 회로의 차단기처럼, 백엔드 다운 / 느림 시 호출 자체를 차단 → 빠른 실패 + 백엔드 회복 시간. 3가지 상태 | 상태 | 의미 | …
[Concurrency] Connection Pool: DB, HTTP, gRPC 의 공통 패턴concurrency
정의 Connection Pool = 연결을 미리 만들어 두고 재사용. 매 요청마다 TCP + TLS + auth 비용을 회피. Pool 의 동작 = producer (reque…
[Concurrency] Rate Limiting: token bucket, sliding windowconcurrency
정의 Rate Limiting = 시간당 요청 수 제한. API 보호, fair use, 비용 제어, DDoS 완화. 5가지 알고리즘 1. Fixed Window - 단순. - …
[Distributed] Kafka Consumer Group: rebalancing, offset, lagdistributed-systems
정의 Consumer Group = 같은 를 가진 consumer 들이 함께 한 topic 을 분담 소비. partition 단위로 분배. Partition 할당 규칙: - pa…
[Network] HTTP/2: multiplexing, HPACK, server push의 종말network
정의 HTTP/2 (2015, RFC 9113) 는 HTTP/1.1 의 문법은 유지하되 전송을 바이너리 + 멀티플렉싱 으로 바꾼다. Head-of-Line Blocking (HT…
TCPnetwork
정의 TCP (Transmission Control Protocol)는 신뢰성 있는 연결 지향 전송 계층 프로토콜이다. RFC 9293 으로 정의되어 있다. HTTP/1.1·2,…

💬 댓글

사이트 검색 / 명령어

검색

스크롤 = 확대/축소 · 드래그 = 이동 · 0 = 원래 크기 · ESC = 닫기