Elixir & Phoenix: Scalable Backend Development · บทเรียน

GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ

สร้างสายการประมวลผลข้อมูลที่ขับเคลื่อนด้วยความต้องการด้วย GenStage โดยให้ผู้บริโภคควบคุมการไหลผ่านแรงดันย้อนกลับเพื่อป้องกันระบบกระจายไม่ให้รับภาระเกิน

บทเรียน 4 จาก 413 ขั้นตอน

GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
end

Defining 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
end

Subscribing 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}
end

ConsumerSupervisor

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_subscribe and max_demand
  • ConsumerSupervisor parallelizes per-event work
  • Flow builds parallel map/reduce pipelines on top

Backpressure keeps distributed systems stable under load.

เริ่มต้นได้ฟรี

เรียนรู้ Elixir ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ”

สร้างสายการประมวลผลข้อมูลที่ขับเคลื่อนด้วยความต้องการด้วย GenStage โดยให้ผู้บริโภคควบคุมการไหลผ่านแรงดันย้อนกลับเพื่อป้องกันระบบกระจายไม่ให้รับภาระเกิน คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Elixir แบบกระจายและการจัดกลุ่ม
  2. กลยุทธ์ซูเปอร์ไวเซอร์ขั้นสูง
  3. ซูเปอร์ไวเซอร์แบบไดนามิกและ Registry
  4. GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development