You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "backuitist (via GitHub)" <gi...@apache.org> on 2025/03/03 16:01:07 UTC

[D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

GitHub user backuitist created a discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

I really wanted to like Key_Shared but I keep bumping into limitations, the last one being so severe that I'm wondering if this feature was ever designed for such a use case?

Use case is tens of thousands of IoT devices sending a reasonably small amount of telemetries (say 1 per second) that need to be statefully processed. On the publishing side, we have a handful of machines receiving the telemetries and dumping that onto pulsar using batching. To be able to consume using `Key_Shared`, batches must be produced using the `KEY_BASED` batching strategy, meaning that instead of having loads of telemetries per batch I end up with basically no batching - key is device ID and each device produces a mere 1 msg/sec... Since the absence of batching adds a very significant load on the cluster (broker & bookies) I'm wondering if this is just not the appropriate use case for the feature? Or... am I doing something wrong?

GitHub link: https://github.com/apache/pulsar/discussions/24044

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "SantanuKar43 (via GitHub)" <gi...@apache.org>.
GitHub user SantanuKar43 added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> then use consistent hashing based on the pod index to select the partitions to consume in each member pod of the statefulset.

@lhotari could you please elaborate on this or share any resources on how to do this?



GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12626934

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> Thanks, I actually came up with that complex design, using readers and key hash ranges. That said, I haven't tested this strategy thoroughly just yet, do you foresee any major road block (esp. w.r.t. key hash ranges)?

That wouldn't solve the batching inefficiency issue which you brought up in the question. Replacing consumers with readers is one potentially a solution in certain cases where consuming state is not handled by Pulsar at all. However, that has many drawbacks regarding message retention and backlog handling.

The Kubernetes Statefulset solution would also work with the `STICKY` mode where key hash ranges are assigned based on pod index and number of pods in the stateful set. However, that's a totally different matter.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12629510

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> Hey thanks for the pointers @lhotari and sorry for the delay. I've had a brief look, how would this work when pods are added/removed?

@backuitist The gist contains a comment in the javadoc "When scaling the StatefulSet, to maintain ordering guarantees, it's necessary to first scale the replicas to 0 to stop all pods, and then change the replica count to the desired value after all pods have stopped. This prevents message reordering that could occur during scaling operations."

It would be possible to improve the solution to handle this dynamically too, but that would require a more complex design.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12628799

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari edited a comment on the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> Use case is tens of thousands of IoT devices sending a reasonably small amount of telemetries (say 1 per second) that need to be statefully processed. On the publishing side, we have a handful of machines receiving the telemetries and dumping that onto pulsar using batching. To be able to consume using Key_Shared, batches must be produced using the KEY_BASED batching strategy, meaning that instead of having loads of telemetries per batch I end up with basically no batching - key is device ID and each device produces a mere 1 msg/sec... Since the absence of batching adds a very significant load on the cluster (broker & bookies) I'm wondering if this is just not the appropriate use case for the feature? Or... am I doing something wrong?

You are right, that it's not optimal when batching isn't used. One possible solution is to calculate an intermediate sharding key to reduce the cardinality significantly so that batching would happen. A gut feeling is that there would have to be fairly low number of these sharding keys so that it would actually be helpful in enabling batching.

For such high volume use cases, it could be better to use partitioned topics with failover consumers, and then have a single consumer per partition. (For strict ordering, it might be necessary to use exclusive subscription to workaround https://github.com/apache/pulsar/issues/15189)

If you happen to be using Reactive Spring & Spring Pulsar Reactive, there's an alternative for Key_Shared subscriptions which works with Failover subscriptions, retaining key order.
Reference of [Reactive Message Consumption with Spring Pulsar](https://docs.spring.io/spring-pulsar/docs/current/reference/reference/reactive-pulsar/reactive-message-consumption.html#reactive-concurrency):

> However, when handling messages one-by-one, concurrency can be specified to increase processing throughput. Simply set the `concurrency` property on `@ReactivePulsarListener`. Additionally, when `concurrency > 1` you can ensure messages are ordered by key and therefore sent to the same handler by setting `useKeyOrderedProcessing = "true"` on the annotation.

In pulsar-client-reactive ReactiveMessagePipelineBuilder, it's this option:
[https://github.com/apache/pulsar-client-reactive/blob/a93b096a10b9451eaec2046b346e[…]/pulsar/reactive/client/api/ReactiveMessagePipelineBuilder.java](https://github.com/apache/pulsar-client-reactive/blob/a93b096a10b9451eaec2046b346e116c040e0fcd/pulsar-client-reactive-api/src/main/java/org/apache/pulsar/reactive/client/api/ReactiveMessagePipelineBuilder.java#L122-L127)

Under the covers it uses Project Reactor's groupBy operator to ensure that processing happens in key-order. The benefit over plain failover subscription is that you have tunable concurrency level, which can go to 100s with low resource consumption. This actually better than Key_Shared subscriptions for certain use cases where processing itself is not costly but most of the work is performed in external API backends. That is the sweet spot for Reactive Spring and Spring Pulsar Reactive / pulsar-client-reactive. There are examples in https://github.com/lhotari/reactive-iot-backend-ApacheCon2021 and https://github.com/lhotari/reactive-pulsar-showcase of using Pulsar Reactive Client.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12491790

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari edited a comment on the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

@SantanuKar43 I have generated a Java class file and k8s yaml for demonstrating this approach with Claude AI. It hasn't been tested. https://gist.github.com/lhotari/99b650bd13de90eb3415bbe94283779b

To increase parallelism in each pod while retaining key based ordering, Pulsar Reactive Client's ReactiveMessagePipelineBuilder could be used instead of ordinary Pulsar Client Consumer. 

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12628015

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> For such high volume use cases, it could be better to use partitioned topics with failover consumers, and then have a single consumer per partition. (For strict ordering, it might be necessary to use exclusive subscription to workaround https://github.com/apache/pulsar/issues/15189)

Kubernetes StatefulSet is one way to have a fixed set of consumers and then use consistent hashing based on the pod index to select the partitions to consume in each member pod of the statefulset.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12625259

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari edited a comment on the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

@SantanuKar43 I have generated a Java class file and k8s yaml for demonstrating this approach with Claude AI. It hasn't been tested. https://gist.github.com/lhotari/99b650bd13de90eb3415bbe94283779b
 
To increase parallelism in each pod while retaining key based ordering, Pulsar Reactive Client's ReactiveMessagePipelineBuilder could be used instead of ordinary Pulsar Client Consumer. 

@backuitist You might also be interested to hear about this solution alternative for Key_Shared to handle the use case that you described.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12628015

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

> Use case is tens of thousands of IoT devices sending a reasonably small amount of telemetries (say 1 per second) that need to be statefully processed. On the publishing side, we have a handful of machines receiving the telemetries and dumping that onto pulsar using batching. To be able to consume using Key_Shared, batches must be produced using the KEY_BASED batching strategy, meaning that instead of having loads of telemetries per batch I end up with basically no batching - key is device ID and each device produces a mere 1 msg/sec... Since the absence of batching adds a very significant load on the cluster (broker & bookies) I'm wondering if this is just not the appropriate use case for the feature? Or... am I doing something wrong?

You are right, that it's not optimal when batching isn't used. One possible solution is to calculate an intermediate sharding key to reduce the cardinality significantly so that batching would happen. A gut feeling is that there would have to be fairly low number of these sharding keys so that it would actually be helpful in enabling batching.

For such high volume use cases, it could be better to use partitioned topics with failover consumers, and then have a single consumer per partition.

If you happen to be using Reactive Spring & Spring Pulsar Reactive, there's an alternative for Key_Shared subscriptions which works with Failover subscriptions, retaining key order.
Reference of [Reactive Message Consumption with Spring Pulsar](https://docs.spring.io/spring-pulsar/docs/current/reference/reference/reactive-pulsar/reactive-message-consumption.html#reactive-concurrency):

> However, when handling messages one-by-one, concurrency can be specified to increase processing throughput. Simply set the `concurrency` property on `@ReactivePulsarListener`. Additionally, when `concurrency > 1` you can ensure messages are ordered by key and therefore sent to the same handler by setting `useKeyOrderedProcessing = "true"` on the annotation.

In pulsar-client-reactive ReactiveMessagePipelineBuilder, it's this option:
[https://github.com/apache/pulsar-client-reactive/blob/a93b096a10b9451eaec2046b346e[…]/pulsar/reactive/client/api/ReactiveMessagePipelineBuilder.java](https://github.com/apache/pulsar-client-reactive/blob/a93b096a10b9451eaec2046b346e116c040e0fcd/pulsar-client-reactive-api/src/main/java/org/apache/pulsar/reactive/client/api/ReactiveMessagePipelineBuilder.java#L122-L127)

Under the covers it uses Project Reactor's groupBy operator to ensure that processing happens in key-order. The benefit over plain failover subscription is that you have tunable concurrency level, which can go to 100s with low resource consumption. This actually better than Key_Shared subscriptions for certain use cases where processing itself is not costly but most of the work is performed in external API backends. That is the sweet spot for Reactive Spring and Spring Pulsar Reactive / pulsar-client-reactive. There are examples in https://github.com/lhotari/reactive-iot-backend-ApacheCon2021 and https://github.com/lhotari/reactive-pulsar-showcase of using Pulsar Reactive Client.

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12491790

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "backuitist (via GitHub)" <gi...@apache.org>.
GitHub user backuitist added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

I might have missed something, but do I have to keep the `KEY_BASED` batching strategy when using key hash ranges?

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12629568

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "backuitist (via GitHub)" <gi...@apache.org>.
GitHub user backuitist added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

Hey thanks for the pointers @lhotari and sorry for the delay. I've had a brief look, how would this work when pods are added/removed?

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12628565

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

@SantanuKar43 I have generated a Java class file and k8s yaml for demonstrating this approach with Claude AI. It hasn't been tested. https://gist.github.com/lhotari/99b650bd13de90eb3415bbe94283779b

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12628015

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "backuitist (via GitHub)" <gi...@apache.org>.
GitHub user backuitist added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

Thanks, I actually came up with that complex design, using readers and key hash ranges. That said, I haven't tested this strategy thoroughly just yet, do you foresee any major road block (esp. w.r.t. key hash ranges)? 

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12629092

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

@backuitist Yes, `KEY_BASED` batching strategy is required when using key hash ranges. 

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12631171

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality [pulsar]

Posted by "backuitist (via GitHub)" <gi...@apache.org>.
GitHub user backuitist added a comment to the discussion: Is Key_Shared suitable for stateful stream processing of small throughput and high cardinality

Ouch... ok, this could be perhaps documented better... I'll try to submit a PR for it.

> However, that has many drawbacks regarding message retention and backlog handling.

Are there other drawbacks beside having to configure data retention appropriately?

GitHub link: https://github.com/apache/pulsar/discussions/24044#discussioncomment-12631630

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org