0Pricing
Scala for Backend Engineering & Functional Programming · レッスン

バックプレッシャー

高速なプロデューサーを安全に扱います。

「バックプレッシャー」はCoddyKit上の無料Scala for Backend Engineering & Functional Programmingレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはScala for Backend Engineering & Functional Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What Is Backpressure?

Backpressure is a flow-control mechanism that prevents a fast producer from overwhelming a slow consumer. Instead of buffering unboundedly or dropping data, the consumer signals how much it can handle.

Akka Streams implements the Reactive Streams standard, where demand flows upstream and elements flow downstream.

Demand-Driven Flow

Each stage only emits when the next stage has signalled demand. A Sink requests N elements; that demand propagates upstream until a Source produces exactly what was asked for.

This pull-based protocol means producers never push more than consumers can process.

Why It Matters for Pipelines

Without backpressure, a fast Kafka consumer feeding a slow database would accumulate millions of in-flight records, exhausting memory and crashing the process.

Backpressure naturally throttles the upstream to the slowest stage, giving stable memory usage under load.

// Fast source, slow sink: backpressure slows the source
val g =
  Source(1 to 1000000)
    .map(_ * 2)
    .to(slowDatabaseSink)

Internal Buffering

Between asynchronous boundaries, Akka Streams keeps a small internal buffer (default 16 elements). It absorbs short bursts so stages need not lock-step on every element.

When the buffer fills, backpressure kicks in and the upstream stops producing until space frees up.

import akka.stream.Attributes

val buffered =
  Flow[Int]
    .map(identity)
    .addAttributes(Attributes.inputBuffer(initial = 32, max = 32))

Explicit buffer with Overflow Strategy

The buffer operator inserts an explicit buffer of a chosen size with an OverflowStrategy that decides what happens when it is full.

This lets you trade memory for the ability to decouple producer and consumer speed.

import akka.stream.OverflowStrategy

val withBuffer =
  Source(1 to 1000)
    .buffer(size = 100, OverflowStrategy.backpressure)

Overflow Strategies

Strategies include backpressure (slow the upstream), dropHead/dropTail (discard oldest or newest), dropBuffer, dropNew, and fail (terminate with an error).

Dropping strategies suit live data like sensor readings where stale values can be discarded safely.

import akka.stream.OverflowStrategy

val latestWins =
  liveTicks.buffer(1, OverflowStrategy.dropHead)

val strict =
  liveTicks.buffer(50, OverflowStrategy.fail)

Conflate to Summarize

When a consumer is slow, conflate merges pending elements into one using a combine function instead of buffering them all.

For example, collapse many numeric updates into their sum, so the consumer always sees an aggregate of what it missed.

val summarized =
  fastMetrics
    .conflate((acc, next) => acc + next)

// Slow downstream receives summed batches

Expand to Fill Demand

expand is the dual of conflate: when downstream demands faster than upstream produces, it synthesizes extra elements from the last seen value.

This is useful to keep emitting a most-recent reading at a steady rate.

val repeated =
  sensor.expand(last => Iterator.continually(last))

// Downstream always gets the latest sensor value

Async Boundaries

By default, fused stages run on a single actor with no buffering between them. Inserting async places a stage on its own actor, adding a buffer and enabling pipelined parallelism.

Async boundaries are where backpressure buffers actually live.

val pipelined =
  Source(1 to 1000)
    .map(slowStep).async
    .map(anotherSlowStep).async
    .to(Sink.ignore)

Throttle as Explicit Rate Control

throttle imposes a deliberate maximum rate, generating backpressure upstream to honor it. This protects rate-limited external services even when the consumer could go faster.

A burst parameter allows short spikes above the steady rate.

import scala.concurrent.duration._

val limited =
  requests
    .throttle(
      elements = 100, per = 1.second, maximumBurst = 20,
      akka.stream.ThrottleMode.Shaping)

Observing Backpressure

You can detect backpressure by watching for upstream slowdowns or by measuring buffer occupancy. The log operator and Akka's stream attributes help trace where a pipeline stalls.

A persistently full buffer indicates the slowest stage that is gating throughput.

val traced =
  Source(1 to 100)
    .log("after-source")
    .map(_ * 2)
    .log("after-map")
    .to(Sink.ignore)

Quick Check

Think about how Akka Streams keeps a fast producer from flooding a slow consumer.

Recap

Backpressure is the demand-driven backbone of Akka Streams: consumers signal demand upstream so producers cannot overwhelm them, keeping memory bounded.

You saw internal buffers, the explicit buffer operator with overflow strategies, summarizing with conflate, filling demand with expand, async boundaries, and deliberate rate control via throttle. Next you will run a complete pipeline.

よくある質問

「バックプレッシャー」レッスンは無料ですか?

はい。「バックプレッシャー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Scala for Backend Engineering & Functional Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。

「バックプレッシャー」で何を学びますか?

高速なプロデューサーを安全に扱います。 ブラウザで直接実行するハンズオンコードでScala for Backend Engineering & Functional Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Scala for Backend Engineering & Functional Programmingを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのScala for Backend Engineering & Functional Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「バックプレッシャー」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このScala for Backend Engineering & Functional Programmingレッスンでコードを書いて実行できますか?

はい。すべてのScala for Backend Engineering & Functional Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Source、Flow、Sink
  2. ストリームを変換する
  3. バックプレッシャー
  4. パイプラインを実行する
← Scala for Backend Engineering & Functional Programmingに戻る