0Pricing
GraphQL APIs with Spring Boot · Aula

Filtragem e escalabilidade de assinaturas

Vá além das assinaturas básicas: entregue apenas os eventos relevantes para cada cliente com filtragem no servidor e escale as assinaturas entre várias instâncias do Spring Boot.

Filtragem e escalabilidade de assinaturas é uma aula grátis de GraphQL APIs with Spring Boot 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 GraphQL APIs with Spring Boot, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.

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

Not Every Client Wants Everything

A naive subscription pushes every event to every subscriber. But a user watching order #42 does not care about order #99.

Filtering ensures each subscriber only receives the events relevant to them.

Subscription Arguments

Subscriptions can take arguments just like queries. A client passes the ID it cares about, and the server uses it to filter the stream.

type Subscription {
  orderUpdated(orderId: ID!): Order
}

Reactive Streams Recap

Spring for GraphQL represents a subscription as a Reactor Flux, an asynchronous stream of items. Filtering is just stream operations applied to that Flux.

Filtering with filter()

Apply .filter() to the publisher so only matching events flow to the subscriber.

@SubscriptionMapping
public Flux<Order> orderUpdated(@Argument String orderId) {
    return orderPublisher.flux()
        .filter(o -> o.getId().equals(orderId));
}

A Shared Event Sink

Use a Reactor Sinks.Many as a hub. Your service emits events into it, and every subscription builds a filtered view of its stream.

private final Sinks.Many<Order> sink =
    Sinks.many().multicast().onBackpressureBuffer();

Emitting Events

When your business logic changes an order, push it into the sink so all matching subscribers are notified.

public void updateOrder(Order order) {
    repository.save(order);
    sink.tryEmitNext(order);
}

The Scaling Problem

An in-memory sink only knows about events on its own JVM. With multiple Spring Boot instances behind a load balancer, an event emitted on instance A never reaches subscribers connected to instance B.

External Pub/Sub to the Rescue

Route events through an external broker like Redis Pub/Sub or Kafka. Every instance publishes to and subscribes from the broker, so all instances see all events.

Bridging Redis to a Flux

Subscribe to a Redis channel and feed incoming messages into the same Flux your GraphQL subscription exposes, unifying local and remote events.

redisTemplate.listenToChannel("orders")
    .map(msg -> deserialize(msg.getMessage()))
    .subscribe(sink::tryEmitNext);

Backpressure and Cleanup

Slow clients can fall behind. Use a bounded buffer or drop strategy, and ensure resources are released when a client disconnects so memory does not leak.

Sinks.many().multicast().onBackpressureBuffer(1024, false);

Best Practices

Keep subscriptions efficient:

  • Filter on the server, never push everything
  • Use an external broker to scale across instances
  • Handle backpressure for slow consumers
  • Clean up on disconnect to avoid leaks

Quick Check

Test your subscription scaling knowledge.

Recap

You scaled real-time subscriptions:

  • Filter streams with .filter() using subscription arguments
  • Use a Sinks.Many hub to broadcast events
  • Route through Redis or Kafka to scale across instances
  • Manage backpressure and clean up on disconnect

Filtered, distributed subscriptions deliver the right data at scale.

Perguntas Frequentes

A aula “Filtragem e escalabilidade de assinaturas” é grátis?

Sim — o texto completo de “Filtragem e escalabilidade de assinaturas” é 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 GraphQL APIs with Spring Boot, atualize para CoddyKit PRO. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.

O que vou aprender em “Filtragem e escalabilidade de assinaturas”?

Vá além das assinaturas básicas: entregue apenas os eventos relevantes para cada cliente com filtragem no servidor e escale as assinaturas entre várias instâncias do Spring Boot. Você pratica GraphQL APIs with Spring Boot 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 GraphQL APIs with Spring Boot?

Nenhuma experiência prévia é necessária. GraphQL APIs with Spring Boot 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 “Filtragem e escalabilidade de assinaturas”?

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 GraphQL APIs with Spring Boot?

Sim. Cada aula de GraphQL APIs with Spring Boot 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. Entendendo as Assinaturas GraphQL
  2. Implementando Atualizações em Tempo Real
  3. Integrando WebSockets ao Spring
  4. Filtragem e escalabilidade de assinaturas
← Voltar para GraphQL APIs with Spring Boot