0Pricing
WebSockets & Real-Time Systems with Spring · Aula

Gerenciamento da contrapressão em fluxos reativos

Gerencie produtores rápidos e clientes WebSocket lentos no WebFlux usando operadores de contrapressão do Reactor, para manter os fluxos estáveis sob carga.

Gerenciamento da contrapressão em fluxos reativos é uma aula grátis de WebSockets & Real-Time Systems with Spring no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de WebSockets & Real-Time Systems with Spring, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

The Fast Producer, Slow Consumer Problem

A reactive WebSocket may emit market ticks faster than a client can consume them. Without control, buffers grow until memory is exhausted. This mismatch is what backpressure solves.

What Backpressure Means

Backpressure is the consumer telling the producer how much it can handle. In Reactor, the subscriber requests n items; the publisher must not exceed that demand.

Reactor Is Demand-Driven

A reactive Flux built from a cold source naturally honors demand: nothing is produced until requested. The challenge appears with hot, time-driven sources like a price feed that emits regardless of demand.

WebSocketHandler Returns a Mono

In WebFlux a handler wires the outbound Flux into session.send. The framework subscribes and applies the transport's demand for you.

public Mono<Void> handle(WebSocketSession session) {
  Flux<String> out = prices.map(p -> session.textMessage(p));
  return session.send(out);
}

onBackpressureBuffer

Buffer overflow items up to a limit, then take an action. Good when bursts are short.

flux.onBackpressureBuffer(1000,
  dropped -> log.warn("dropped {}", dropped),
  BufferOverflowStrategy.DROP_OLDEST);

onBackpressureDrop

When the consumer is slow, simply drop new items. Ideal for telemetry where only the latest values matter.

flux.onBackpressureDrop(dropped -> metrics.increment("dropped"));

onBackpressureLatest

Keep only the most recent item, discarding intermediate ones. Perfect for a live dashboard that shows the current value, not the history.

flux.onBackpressureLatest();

Sampling and Throttling

Instead of dropping reactively, reduce the rate up front. sample emits the latest value at a fixed interval, smoothing a firehose into a manageable stream.

flux.sample(Duration.ofMillis(200));

Bounding Buffers Everywhere

Unbounded buffers are the silent killer. Always cap buffers and choose a strategy (drop, error, latest) so a stalled client cannot consume the server's heap.

Detecting Overwhelmed Clients

If a client repeatedly triggers drops, it may be too slow for the feed. Consider lowering its update rate, sending deltas, or closing the session with a clear status.

session.close(CloseStatus.create(1011, "client too slow"));

Choosing a Strategy

Match the operator to the data:

  • Must-not-lose orders → buffer with a safe cap, or error
  • Live metrics/prices → latest or sample
  • Best-effort telemetry → drop

Quick Check

Test your backpressure understanding.

Recap

You tamed reactive streams:

  • Backpressure aligns a fast producer with a slow consumer
  • Reactor is demand-driven; hot sources need explicit handling
  • buffer, drop, and latest strategies suit different data
  • sample throttles a firehose at the source
  • Never leave buffers unbounded; close clients that cannot keep up

Perguntas Frequentes

A aula “Gerenciamento da contrapressão em fluxos reativos” é grátis?

Sim — o texto completo de “Gerenciamento da contrapressão em fluxos reativos” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de WebSockets & Real-Time Systems with Spring, atualize para CoddyKit PRO. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.

O que vou aprender em “Gerenciamento da contrapressão em fluxos reativos”?

Gerencie produtores rápidos e clientes WebSocket lentos no WebFlux usando operadores de contrapressão do Reactor, para manter os fluxos estáveis sob carga. Você pratica WebSockets & Real-Time Systems with Spring com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar WebSockets & Real-Time Systems with Spring?

Nenhuma experiência prévia é necessária. WebSockets & Real-Time Systems with Spring no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Gerenciamento da contrapressão em fluxos reativos”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de WebSockets & Real-Time Systems with Spring?

Sim. Cada aula de WebSockets & Real-Time Systems with Spring inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Introdução à programação reativa
  2. Manipuladores WebSocket do WebFlux
  3. Construção de serviços reativos em tempo real
  4. Gerenciamento da contrapressão em fluxos reativos
← Voltar para WebSockets & Real-Time Systems with Spring