Question: If I'm doing event sourcing, without a s...
# general
d
Question: If I'm doing event sourcing, without a snapshot, how might I be able to load events for a given aggregate Id, to build aggregate state? I see that reader has the ability to use HashKeys, but as the db size grows, hashes will collide with other keys and load those objects back as well, which will slow performance. Is there a better way to do this that anyone is doing? I also see there's an option to copy events to a secondary storage. However, this could cause race conditions, because the events might not be there right away. Also it forces me to store twice as much data. Has anyone figured out the best way to handle this just using pulsar?
l
This is a very good question.
If I'm doing event sourcing, without a snapshot, how might I be able to load events for a given aggregate Id, to build aggregate state?
I see that reader has the ability to use HashKeys, but as the db size grows, hashes will collide with other keys and load those objects back as well, which will slow performance. Is there a better way to do this that anyone is doing?
Although the Pulsar Reader API enables this functionality, it becomes a performance bottleneck at high scale. You don't need to worry about performance if your use case operates at a limited scale. One of the key performance challenges is that even when you can filter messages with the Reader API, Pulsar must read all entries for a specific topic from BookKeeper (or Tiered Storage/S3) since there's no index for keys in the storage (it's a log, not a database). When the Pulsar topic grows in the number of events, the overhead increases proportionally. This isn't a scalable solution. When using a single shared topic for all aggregates, this approach has linear degradation. As your system grows from handling 1,000 aggregates to 1,000,000 aggregates, reading any single aggregate requires 1,000x more resources on the Pulsar Broker and BookKeeper side (assuming all aggregates have the same number of events). At high scale, there needs to be a way to split the events for a specific aggregate ID into a specific topic so that only a limited number of aggregate IDs are stored in a single topic. The simplest solution is to use a partitioned topic with a large number of partitions. This would use the hash of the aggregate ID to choose the topic's partition. However, this also has limits since you'd have to set a fixed number of partitions at the beginning, and performance would likewise degrade over time. For example, if you started with 100 partitions and the system grows from 1,000 to 1,000,000 aggregates, it would now require 10x (instead of 1,000x) more resources for loading each aggregate by ID. That continues to be a scalability problem. Designing a sharding strategy that can scale for this aggregate loading use case is essential. Pulsar supports up to about 1 million topics with ZooKeeper under certain conditions, and more when using Oxia for the metadata store (also under certain conditions, such as limiting the amount of topics per namespace since Admin APIs don't support pagination). In certain use cases, it might be possible to use an individual topic for each aggregate ID. Alternatively, there might already be an associated grouping ID for each aggregate ID that could be used as a useful way to reduce cardinality. This solution has its own limits when the number of aggregates can grow very high (hundreds of thousands) or when the grouping ID results in many aggregate IDs that each contain many events. When the above approaches aren't suitable, one option is to use consistent hashing based sharding that can be expanded later by adding resharding support. When more shards are added, consistent hashing helps minimize the movement of existing aggregate id events from old topics to new topics. There could be a maintenance process that cleans up old topics, replaces them with new topics, and removes events that no longer belong to that "shard." An alternative to consistent hashing could be a custom mapping from an aggregate ID to its event topic, which would provide significant flexibility in event topic allocation. This would require a database for storing this mapping. Expandable sharding approaches are more scalable and flexible than relying on a single partitioned topic. Some event sourcing solutions also require deleting events due to regulatory reasons such as GDPR. Besides sharding and resharding, handling event deletion could also complicate the solution. For large-scale event sourcing systems that require loading events for a specific aggregate ID with Pulsar, it would be useful to build microservices that handle this complexity rather than exposing the Pulsar client directly to applications. Such microservices can be built with Spring Boot and Quarkus, both of which have first-class support for Pulsar and various ways to expose event streams (for example, Server-Sent Events (SSE) over REST, ndjson over REST, event streams over gRPC, etc.).
I also see there's an option to copy events to a secondary storage. However, this could cause race conditions, because the events might not be there right away. Also it forces me to store twice as much data.
That's correct, however data duplication can be an effective strategy in many cases. Complexity related to such race conditions could be handled in the microservices layer.
Has anyone figured out the best way to handle this just using pulsar?
The "best way" depends heavily on the scale of the solution. Pulsar is used for both small and large-scale use cases. In some large-scale use cases, it's necessary to have solutions that can scale when usage grows 10x, 100x, or 1,000x. I hope this answer provides some guidance for addressing your use case. What scale is your use case targeting? I also hope that more Pulsar users will share their experiences of successful strategies for event sourcing use cases at both small and large scales. I believe that most users have avoided using Pulsar as an event store where there would be a requirement for loading events for a specific aggregate ID individually and therefore wouldn't face the scalability challenges mentioned in my detailed description. The main scalability challenge in loading aggregate events by ID comes from workloads that query a Pulsar topic where each query reads many events. When the system usage grows, this load becomes unmanageable at scale. Avoiding this scenario in one way or another is necessary.
z
@mayura312