GenStage와 백프레셔 파이프라인
GenStage로 수요 중심 데이터 파이프라인을 구축하는 방법을 배웁니다. 소비자가 백프레셔를 통해 흐름을 제어하여 분산 시스템의 과부하를 방지합니다.
GenStage와 백프레셔 파이프라인은(는) CoddyKit의 무료 Elixir & Phoenix: Scalable Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Elixir & Phoenix: Scalable Backend Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
The Overload Problem
When a fast producer feeds a slow consumer, messages pile up and memory explodes. Backpressure solves this: consumers signal how much work they can handle, and producers send only that much.
What is GenStage?
GenStage is an Elixir behaviour for building staged, demand-driven pipelines. Data flows from producers to consumers, with optional producer-consumer stages in between.
The Three Stage Types
Every GenStage process plays one role:
- Producer: emits events on demand
- ProducerConsumer: receives, transforms, and re-emits
- Consumer: receives and processes events
Defining a Producer
A producer implements init/1 and handle_demand/2. It returns events only when consumers ask for them.
defmodule Counter do
use GenStage
def init(start), do: {:producer, start}
def handle_demand(demand, state) do
events = Enum.to_list(state..(state + demand - 1))
{:noreply, events, state + demand}
end
endDefining a Consumer
A consumer implements handle_events/3. It returns no events itself, only acknowledging the demand it processed.
defmodule Printer do
use GenStage
def init(_), do: {:consumer, :ok}
def handle_events(events, _from, state) do
for e <- events, do: IO.inspect(e)
{:noreply, [], state}
end
endSubscribing Stages
Connect a consumer to a producer with GenStage.sync_subscribe/2. The max_demand option caps how many events flow at once.
{:ok, prod} = GenStage.start_link(Counter, 0)
{:ok, cons} = GenStage.start_link(Printer, :ok)
GenStage.sync_subscribe(cons, to: prod, max_demand: 10)How Backpressure Works
The consumer requests up to max_demand events. The producer can never send more than was requested, so a slow consumer naturally throttles a fast producer. No buffering blowup.
Producer-Consumer Stages
A middle stage transforms data. It implements handle_events/3 but returns transformed events for the next stage.
def handle_events(events, _from, state) do
doubled = Enum.map(events, &(&1 * 2))
{:noreply, doubled, state}
endConsumerSupervisor
For concurrent processing, ConsumerSupervisor spawns a short-lived child process per event, bounded by demand. This parallelizes work while keeping backpressure intact.
Flow for Parallel Pipelines
The Flow library builds on GenStage to offer map/reduce-style parallel data processing with partitioning — ideal for crunching large collections across cores.
File.stream!("big.csv")
|> Flow.from_enumerable()
|> Flow.map(&parse_line/1)
|> Flow.partition()
|> Enum.to_list()When to Reach for GenStage
Use GenStage when you have:
- A rate mismatch between data source and processing
- Streaming data that must not overwhelm memory
- Multi-stage transformation pipelines
Quick Check
Test your GenStage knowledge.
Recap
You learned demand-driven pipelines:
- GenStage has producers, producer-consumers, and consumers
- Consumers request bounded demand for backpressure
- Subscribe stages with
sync_subscribeandmax_demand ConsumerSupervisorparallelizes per-event workFlowbuilds parallel map/reduce pipelines on top
Backpressure keeps distributed systems stable under load.
자주 묻는 질문
“GenStage와 백프레셔 파이프라인” 강의는 무료인가요?
네 — “GenStage와 백프레셔 파이프라인” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Elixir & Phoenix: Scalable Backend Development 강의 전체를 잠금 해제할 수 있습니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“GenStage와 백프레셔 파이프라인”에서 뭘 배우나요?
GenStage로 수요 중심 데이터 파이프라인을 구축하는 방법을 배웁니다. 소비자가 백프레셔를 통해 흐름을 제어하여 분산 시스템의 과부하를 방지합니다. 브라우저에서 직접 실행하는 실습 코드로 Elixir & Phoenix: Scalable Backend Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Elixir & Phoenix: Scalable Backend Development을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Elixir & Phoenix: Scalable Backend Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“GenStage와 백프레셔 파이프라인” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Elixir & Phoenix: Scalable Backend Development 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Elixir & Phoenix: Scalable Backend Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 분산 Elixir와 클러스터링
- 고급 감독 전략
- 동적 감독자와 레지스트리
- GenStage와 백프레셔 파이프라인