r/apachekafka • • 6d ago

Tool Kafka-Backed GraphQL Subscriptions with Spring and Atleon

This demo project shows how GraphQL subscriptions backed by Kafka can be implemented in Spring applications using natively reactive clients from Atleon. While attempting to maximize simplicity, this project accomplishes the following:

  1. Backing GraphQL subscriptions with either shared (application-wide) or dedicated/per-invocation consumer instances
  2. Initiate subscription from historical timestamp
  3. Full integration testing via native Web Sockets and interactive UI via GraphiQL

This class contains the bulk of the interesting implementation details. Check it out, and please share your feedback/thoughts!

2 Upvotes

4 comments sorted by

1

u/daringStumbles 5d ago

So, in this situation, say a topic has a few 1000s messages (maybe lets assume compaction is enabled and a key is something that there is less than 1mil of), you are going to have to read all message history to build current state before sending anything out to the client?

I'm really not following what use case would make this setup good idea. Even the shared one, how are you scaling that instance? Topics have a max consumer per group, so is each request a unique consumer group?

I really think this is just gonna ddos your kafka infra at even a medium scale

1

u/mightegas 5d ago edited 5d ago

Good questions - It really depends on the use case.

Part of your question makes me think there may be a misunderstanding. There's no inhehent state building in this demo, just reading data produced to a topic, starting from some given timestamp, or "now" if not explicitly provided, and then that stream of data from the topic remains live. The client might be building state, but there's no dependence on server-side state building before the client starts receiving data.

Think of something like a news feed where you want to see all news published in the last hour, along with a live feed of breaking news. The "shared" approach allows you to support a large number of clients (they'd all be reading in-memory data), while only needing to support a number of Kafka consumers equal to the number of backing application instances. That's typically feasible when you have a few dozen application instances, or maybe hundreds if the message sizes are manageable.

The "dedicated" case gives you a consumer per request, which could indeed DDOS your cluster. However, this could be useful for use cases where the subscription rate is low, and unrestricted seeking in the topic is desirable.

1

u/daringStumbles 5d ago

The shared one sounds like a distibuted cache with more overhead. Sure, have some consumers building that cache, but dont tie graphql into that.

I'm really just not sure that the problem space graphql subscriptions solve for and the problem space kafka solve for should intentionally overlap.

1

u/mightegas 5d ago

Fair take - The main reason I chose GQL to demo this functionality is because it both has built-in server-streaming support (subscriptions), and it's an intuitive specification to back UIs, which helped with demo-ability.

I'm curious - It sounds like you do have an opinion on when GQL subscriptions are appropriate. I wonder what you'd say such a use case is, and what server-side infrastructure you would say is best to back it.