Filtrowanie i skalowanie subskrypcji
Wyjdą Państwo poza podstawowe subskrypcje: będą dostarczać tylko zdarzenia istotne dla danego klienta dzięki filtrowaniu po stronie serwera oraz skalować subskrypcje na wielu instancjach Spring Boot.
Filtrowanie i skalowanie subskrypcji to bezpłatna lekcja GraphQL APIs with Spring Boot na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej GraphQL APIs with Spring Boot, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.Manyhub 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.
Ucz się GraphQL APIs with Spring Boot dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Filtrowanie i skalowanie subskrypcji” jest bezpłatna?
Tak — pełny tekst „Filtrowanie i skalowanie subskrypcji” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu GraphQL APIs with Spring Boot, przejdź na CoddyKit PRO. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Co nauczysz się w „Filtrowanie i skalowanie subskrypcji”?
Wyjdą Państwo poza podstawowe subskrypcje: będą dostarczać tylko zdarzenia istotne dla danego klienta dzięki filtrowaniu po stronie serwera oraz skalować subskrypcje na wielu instancjach Spring Boot. Ćwiczysz GraphQL APIs with Spring Boot z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć GraphQL APIs with Spring Boot?
Nie wymagamy żadnego doświadczenia. GraphQL APIs with Spring Boot w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Filtrowanie i skalowanie subskrypcji”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji GraphQL APIs with Spring Boot?
Tak. Każda lekcja GraphQL APIs with Spring Boot zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Zrozumienie subskrypcji GraphQL
- Implementowanie aktualizacji w czasie rzeczywistym
- Integracja WebSockets ze Spring
- Filtrowanie i skalowanie subskrypcji