0Pricing
GraphQL APIs with Spring Boot · Lesson

Filtering and Scaling Subscriptions

Go beyond basic subscriptions: deliver only the events each client cares about with server-side filtering, and scale subscriptions across multiple Spring Boot instances.

Filtering and Scaling Subscriptions is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the GraphQL APIs with Spring Boot learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Filtering and Scaling Subscriptions” lesson free?

Yes — the full text of “Filtering and Scaling Subscriptions” is free to read here on the web, and the GraphQL APIs with Spring Boot course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the GraphQL APIs with Spring Boot course, upgrade to CoddyKit PRO.

What will I learn in “Filtering and Scaling Subscriptions”?

Go beyond basic subscriptions: deliver only the events each client cares about with server-side filtering, and scale subscriptions across multiple Spring Boot instances. You practise GraphQL APIs with Spring Boot with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start GraphQL APIs with Spring Boot?

No prior experience is required. GraphQL APIs with Spring Boot on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Filtering and Scaling Subscriptions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this GraphQL APIs with Spring Boot lesson?

Yes. Every GraphQL APIs with Spring Boot lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding GraphQL Subscriptions
  2. Implementing Real-time Updates
  3. Integrating WebSockets with Spring
  4. Filtering and Scaling Subscriptions
← Back to GraphQL APIs with Spring Boot