StreamingEventProcessor
, or Streaming Processor for short, is a type of Event Processor. As any Event Processor, it serves as the technical aspect to handle events by invoking the event handlers written in an Axon application.StreamableMessageSource
. The StreamableMessageSource
is an infrastructure component through which we can open a stream of events. The source can also specify positions on the event stream, so-called Tracking Tokens, used as start positions when opening an event stream. An example of a StreamableMessageSource
is the EventStore
, like for example Axon Server or an RDBMS.StreamableMessageSource
. Using separate threads decouples the StreamingEventProcessor
from other operations (e.g., event publication or command handling), allowing for cleaner separation within any application.
Using separate threads allows for parallelization of the event load, either within a single JVM or between several.StreamableMessageSource
. The first time a stream has started, it, by default, will begin at the tail (the oldest/the very first token) of the stream. It keeps track of the event processing progress while traversing the stream. It does so by storing the Tracking Tokens, or tokens for short, accompanying the events. This solution works towards tracking the progress since the tokens specify the event's position on the stream.Head or Tail?The oldest (very first) token is located at the tail of the stream, and the latest (newest) token is positioned at the head of the stream.
Default Event ProcessorWhichEventProcessor
type becomes the default processor depends on the event message source available in your application. In the majority of use cases, an Event Store is present. As the Event Store is a type ofStreamableMessageSource
, the default will switch to the Tracking Event Processor.If the application only has an Event Bus configured, the framework will lack aStreamableMessageSource
. It will fall back to the Subscribing Event Processor as the default in these scenarios. This implementation will use the configuredEventBus
as itsSubscribableMessageSource
.
TrackingEventProcessor
, you can invoke the usingTrackingEventProcessors
method:registerTrackingEventProcessor
is used:.
, use the map notation:TrackingEventProcessorConfiguration
can be used. When invoking the registerTrackingEventProcessor
method, you can provide a tracking processor configuration object, or you can register the configuration instance explicitly:PooledStreamingEventProcessor
, you can invoke the usingPooledStreamingProcessors
method:registerPooledStreamingProcessor
is used:.
, use the map notation:PooledStreamingProcessorConfiguration
can be used. When invoking the registerPooledStreamingEventProcessor
method, you can provide a pooled streaming processor configuration object, or you can register the configuration instance explicitly:TrackingEventProcessor
will retry processing the event using an incremental back-off period. It will start at 1 second and double after each attempt until it reaches the maximum wait time of 60 seconds per attempt. This back-off time ensures that in a distributed environment, when another node is able to process events, it will have the opportunity to claim the token required to process the event.PooledStreamingEventProcessor
simply aborts the failed part of the process. The Pooled Streaming Processor can deal with this since the threading mode is different from the Tracking Processor. As such, the chance is high the failed process will be picked up quickly by another thread within the same JVM. This chance increases further whenever the PSEP instance is distributed over several application instances.TrackingToken
, the "token" for short. Such a token accompanies each message a streaming processor receives through its event stream. It's this token that:TrackingToken
after handling batches of events. Keeping the progress requires CRUD operation, for which the Streaming Processor uses the TokenStore
.TrackingToken
. The processor will update this claim every time it has finished handling a batch of events. This so-called "claim extension" is, just as updating and saving of tokens, delegated to the Token Store. Hence, the Streaming Processors achieves collaboration among instances/threads through token claims.StreamableMessageSource
to retrieve a stream of events that will open on start-up. It requires a TrackingToken
to open this stream, which it will fetch from the TokenStore
. However, if a Streaming Processor starts for the first time, there is no TrackingToken
present to open the stream with yet.A Saga's Streaming Processor initial positionA Streaming Processor dedicated to a Saga will default the initial token to the head of the stream. The default initial token position ensures that the Saga does not react to events from the past, as in most cases, this would introduce unwanted side effects.
TokenStore
has (accidentally) been cleared between application runs, thus losing the stored tokens.InMemoryTokenStore
was used, and hence the processor could never persist the token to begin with.StreamingEventProcessor
is configurable for every processor instance. When configuring the initial token builder function, the received input parameter is the StreamableMessageSource
. The message source, in turn, gives three possibilities to build a token, namely:createHeadToken()
- Creates a token from the head of the event stream.createTailToken()
- Creates a token from the tail of the event stream. Creating tail tokens is the default value for most Streaming Processors.createTokenAt(Instant)
/ createTokenSince(Duration)
- Creates a token that tracks all events after a given time. If there is an event precisely at that given moment in time, it will also be taken into account.StreamableMessageSource
input parameter and create a token by yourself. Consider the following snippets if you want to configure a different initial token:tokenClaimInterval
- Defines how long to wait between attempts to claim a segment. A processor uses this value to steal token claims from other processor threads. This value defaults to 5000 milliseconds.eventAvailabilityTimeout
- Defines the time to "wait for events" before extending the claim. Only the Tracking Event Processor uses this. The value defaults to 1000 milliseconds.claimExtensionThreshold
- Threshold to extend the claim in the absence of events. Only the Pooled Streaming Event Processor uses this. The value defaults 5000 milliseconds.TokenStore
provides the CRUD operations for the StreamingEventProcessor
to interact with TrackingTokens
. The streaming processor will use the store to construct, fetch and claim tokens.InMemoryTokenStore
is used. The InMemoryTokenStore
is not recommended in most production scenarios since it cannot maintain the progress through application shutdowns. Unintentionally using the InMemoryTokenStore
counts towards one of the unexpected scenarios where the framework creates an initial token on each application start-up.TokenStore
implementations:InMemoryTokenStore
- A TokenStore
implementation that keeps the tokens in memory. This implementation does not suffice as a production-ready store in most applications.JpaTokenStore
- A TokenStore
implementation using JPA to store the tokens with. Expects that a table is constructed based on the org.axonframework.eventhandling.tokenstore.jpa.TokenEntry
. It is easily auto-configurable with, for example, Spring Boot.JdbcTokenStore
- A TokenStore
implementation using JDBC to store the tokens with. Expects that the schema is constructed through the JdbcTokenStore#createSchema(TokenTableFactory)
method. Several TokenTableFactory
can be chosen here, like the GenericTokenTableFactory
, PostgresTokenTableFactory
or Oracle11TokenTableFactory
implementation.MongoTokenStore
- A TokenStore
implementation using Mongo to store the tokens with.Where to store Tokens?Where possible, we recommend using a token store that stores tokens in the same database as to where the event handlers update the view models. This way, changes to the view model can be stored atomically with the changed tokens. Furthermore, it guarantees exactly-once processing semantics.
EventProcessingConfigurer
:TokenStore
for all processors:TokenStore
for a specific processor, use:TokenStore
implementation is defined based on dependencies available in Spring Boot, in the following order:TokenStore
bean is defined, that bean is used.EntityManager
is available, the JpaTokenStore
is defined.DataSource
is defined, the JdbcTokenStore
is created.InMemoryToken
store is used.@Configuration
class:EventProcessingConfigurer
, which allows more fine-grained customization:StreamingEventProcessor
to more efficiently process batches of events. As described here, a streaming processor's thread requires a claim on a tracking token to process events.initialSegmentCount
property. Only when a streaming processor starts for the first time can it initialize the number of segments to use. This requirement follows from the fact each token represents a single segment. Tokens, in turn, can only be initialized if they are not present yet, as is explained in more detail here.TrackingEventProcessor
is one.TrackingEventProcessor
is one.PooledStreamingEventProcessor
is sixteen.PooledStreamingEventProcessor
is sixteen.TrackingEventProcessor
and PooledStreamingEventProcessor
is one and sixteen, respectively.Parallel Processing and Subscribing Event ProcessorsNote that Subscribing Event Processor don't manage their own threads. Therefore, it is not possible to configure how they should receive their events. Effectively, they will always work on a sequential-per-aggregate basis, as that is generally the level of concurrency in the command handling component.
SequencingPolicy
for this. The SequencingPolicy
is a function that returns a value for any given message. If the return value of the SequencingPolicy
function is equal for two distinct event messages, it means that those messages must be processed sequentially. By default, Axon components will use the SequentialPerAggregatePolicy
, making it so that events published by the same aggregate instance will be handled sequentially. Check out this section to understand how to influence the sequencing policy.SequencingPolicy
controls this order. The SequencingPolicy
defines whether events must be handled sequentially, in parallel, or a combination of both. Policies return a sequence identifier of a given event.SequencingPolicy
returns a different value for two events, they may be processed concurrently. Note that if the policy returns a null
sequence identifier, the event may be processed in parallel with any other events.** Parallel Processing and Sagas**A saga instance is never invoked concurrently by multiple threads. Therefore, theSequencingPolicy
is irrelevant for a saga. Axon will ensure each saga instance receives the events it needs to process in the order they have been published on the event bus.
SequencingPolicy
decides whether an event belongs to a given segment. Furthermore, Axon guarantees that Events that are part of the same segment are processed sequentially.SequentialPerAggregatePolicy
- The default policy. It will force domain events that were raised from the same aggregate to be handled sequentially. Thus, events from different aggregates may be handled concurrently. This policy is typically suitable for Event Handling Components that update details from aggregates in databases.FullConcurrencyPolicy
- This policy will tell Axon that this Event Processor may handle all events concurrently. This means that there is no relationship between the events that require them to be processed in a particular order.SequentialPolicy
- This policy tells Axon that it can process all events sequentially. Handling of an event will start when the handling of a previous event has finished.PropertySequencingPolicy
- When configuring this policy, the user is required to provide a property name or property extractor function. This implementation provides a flexible solution to set up a custom sequencing policy based on a standard value present in your events. Note that this policy only reacts to properties present in the event class.MetaDataSequencingPolicy
- When configuring this policy, the user is required to provide a metaDataKey
to be used. This implementation provides a flexible solution to set up a custom sequencing policy based on a standard value present in your events' metadata.SequencingPolicy
:SequencingPolicy
in a properties file, we should provide a bean name:SequencingPolicy
interface. This interface defines a single method, getSequenceIdentifierFor(T)
, that returns the sequence identifier for a given event:StreamingEventProcessor
to use several threads. The following section describes the threading differences between the Tracking- and Pooled Streaming Event Processor. These sections are followed up with samples on configuring multiple threads for the TEP and PSEP, respectively.Thread and Segment CountAdjusting the number of threads will not automatically parallelize a Streaming Processor. A segment claim is required to let a thread process any events. Hence, increasing the thread count should be paired with adjusting the segment count.
TrackingEventProcessor
uses a ThreadFactory
to start the process of claiming segments. It will use a single thread per segment it is able to claim until the processor exhausts the configured amount of threads. Each thread will open a stream with the StreamableMessageSource
and start processing events at their own speed. Other segment operations, like split and merge, are processed by the thread owning the segment operated on.PooledStreamingEventProcessor
uses two threads pools instead of the single fixed set of threads used by the TrackingEventProcessor
. The first thread pool is in charge of opening a stream with the event source, claiming as many segments as possible, and delegating all the work.Coordinator
. This coordinator defaults to using a ScheduledExecutorService
with a single thread, which suffices in most scenarios.Coordinator
of the pooled streaming processor could claim. The Coordinator
starts a WorkPackage
for each segment and provides them the events to handle. The work package will, in turn, invoke the Event Handling Components to process the events. These packages run within the second thread pool, the so-called "worker executor" pool. The worker-pool also defaults to ScheduledExecutorService
with a single thread.maxClaimedSegments
is configurable if required (the defaults is Short.MAX
). The fact the TEP can only claim a single segment per thread highlights a problem of that implementation. Events will go unprocessed if there are more segments than threads when using the tracking processor since events belong to a single segment. Furthermore, it makes dynamic scaling tougher since you cannot adjust the number of threads at runtime. Here we see significant benefits for using the PSEP instead of the TEP since it completely drops the "one segment per thread" policy. As such, partial processing is never a problem, the PooledStreamingEventProcessor
would encounter.ScheduledExecutorService
is configurable, which allows sharing the executor between different processor instances. Thus, the PSEP provides a higher level of flexibility towards optimizing the total amount of threads used within an application. The freedom in thread pool configuration is helpful when, for example, the number of different Event Processors in a single application increases.Which Streaming Processor should I use?In most scenarios, thePooledStreamingEventProcessor
is the recommended processor implementation. We conclude this based on the segment-to-thread-count ratio, its ability to share thread pools, and the lower amount of opened event streams.TheTrackingEventProcessor
will still be ideal if you anticipate the processing speed between segments to differ significantly. Also, if the application does not have too many processor instances, the need to share thread pools is loosened.
TokenStore
. By default, it will use the JVM's name (usually a combination of the hostname and process ID) as the nodeId
.StreamingEventProcessor
, with the releaseSegment(int segmentId)
or releaseSegment(int segmentId, long releaseDuration, TimeUnit unit)
methodreleaseSegment
method. When invoking releaseSegment
, the StreamingEventProcessor
will "let go of" the segment for some time.tokenClaimInterval
.StreamingEventProcessor
, with the splitSegment(int segmentId)
and mergeSegment(int segmentId)
methodsStreamingProcessorController
, there are a couple of points to consider. When invoking the split/merge operation on a StreamingEventProcessor
, that processor should be in charge of the segment you want to split or merge. Thus, either the streaming processor already has a claim on the segment(s) or can claim the segment(s). Without the claims, the processor will simply fail the split or merge operation.