Subscription Configuration
Protean provides a flexible configuration system for subscriptions that allows you to configure settings at multiple levels. Configuration follows a priority hierarchy, with more specific settings overriding general ones.
Configuration Priority Hierarchy
Configuration is resolved using this priority order (highest to lowest):
- Handler Meta
subscription_config: Explicit configuration dict on handler - Handler Meta
subscription_type: Type specified on handler - Handler Meta
subscription_profile: Profile specified on handler - Server config handler-specific:
server.subscriptions.HandlerName - Server config defaults:
server.default_subscription_* - Profile defaults: Defaults from the resolved profile
- Hardcoded defaults: Built-in fallback values
Configuration Profiles
Profiles provide pre-configured settings optimized for common scenarios. Using profiles simplifies configuration while still allowing overrides.
Available Profiles
| Profile | Type | Description |
|---|---|---|
production |
Stream | High throughput with reliability guarantees |
fast |
Stream | Low-latency processing |
batch |
Stream | High-volume batch processing |
debug |
Stream | Development and debugging |
projection |
Event Store | Building read models |
Profile Defaults
production Profile
Optimized for production workloads with reliability guarantees:
{
"subscription_type": "stream",
"messages_per_tick": 100,
"tick_interval": 0,
"blocking_timeout_ms": 5000,
"max_retries": 3,
"retry_delay_seconds": 1,
"enable_dlq": True,
"position_update_interval": 10,
"circuit_breaker_threshold": 10,
"circuit_breaker_reset_seconds": 60,
}
fast Profile
Optimized for low-latency processing:
{
"subscription_type": "stream",
"messages_per_tick": 10,
"tick_interval": 0,
"blocking_timeout_ms": 100,
"max_retries": 2,
"retry_delay_seconds": 0,
"enable_dlq": True,
"position_update_interval": 5,
"circuit_breaker_threshold": 10,
"circuit_breaker_reset_seconds": 60,
}
batch Profile
Optimized for high-throughput batch processing:
{
"subscription_type": "stream",
"messages_per_tick": 500,
"tick_interval": 0,
"blocking_timeout_ms": 10000,
"max_retries": 5,
"retry_delay_seconds": 2,
"enable_dlq": True,
"position_update_interval": 50,
"circuit_breaker_threshold": 10,
"circuit_breaker_reset_seconds": 60,
}
debug Profile
Optimized for development and debugging:
{
"subscription_type": "stream",
"messages_per_tick": 1,
"tick_interval": 1,
"blocking_timeout_ms": 1000,
"max_retries": 1,
"retry_delay_seconds": 0,
"enable_dlq": False,
"position_update_interval": 1,
"circuit_breaker_threshold": 10,
"circuit_breaker_reset_seconds": 60,
}
projection Profile
Optimized for building read models:
{
"subscription_type": "event_store",
"messages_per_tick": 100,
"tick_interval": 0,
"position_update_interval": 10,
"enable_dlq": False, # Not supported for event_store
}
Custom Profiles
When the built-in profiles don't match your workload, define your own named
profiles under [server.profiles.<name>] in domain.toml. A custom profile is a
set of subscription config overrides. Once defined, its name is usable anywhere a
built-in name is: default_subscription_profile, a per-handler profile, or a
handler's Meta subscription_profile.
[server.profiles.myfast]
inherits = "fast" # optional: base on a built-in profile
messages_per_tick = 25 # override one (or more) of the inherited fields
[server.subscriptions.NotificationHandler]
profile = "myfast"
Inheritance. With inherits = "<built-in>", the profile starts from that
built-in's defaults and applies your overrides on top. A custom profile
inherits one level only, from a built-in base, so it cannot inherit from
another custom profile and no inheritance cycle can occur. Without inherits,
the profile starts from the framework's hardcoded defaults.
Allowed fields. A custom profile may set inherits plus any of the standard
profile fields: subscription_type, messages_per_tick, tick_interval,
blocking_timeout_ms, max_retries, retry_delay_seconds, enable_dlq,
position_update_interval, origin_stream, circuit_breaker_threshold,
circuit_breaker_reset_seconds.
Validation is fail-fast, a ConfigurationError is raised when the profiles are first
resolved if a custom profile:
- reuses a built-in name (
production,fast,batch,debug,projectionare reserved), - sets a field that isn't in the allowed list above, or
inheritsfrom anything other than a built-in profile.
Precedence is unchanged. A custom profile name takes the priority of the level that names it in the priority hierarchy, exactly like a built-in. When you set a field explicitly at the same level as the profile, your value still overrides the profile's default.
Caveat for default_subscription_profile. A profile named there resolves at
priority 6, which is below the server-level defaults at priority 5
(default_subscription_type, messages_per_tick, tick_interval, and the
[server.stream_subscription] / [server.event_store_subscription] blocks).
When a server-level default sets a field, it overwrites the profile's value.
Protean's built-in configuration sets most of these fields even when
domain.toml does not. As a result, default_subscription_profile sets only
the fields the server-level defaults leave unset, such as
circuit_breaker_threshold. Every built-in profile has the same values as the
hardcoded defaults for those fields, so a built-in profile named there changes
nothing.
To apply a whole profile, name it at a higher level: a per-handler profile
(priority 4) or a handler's Meta subscription_profile (priority 3). Built-in
and custom profiles behave the same way here.
Configuration Options Reference
Each default below is the value a handler gets in a domain with no [server]
settings and no profile. One exception: enable_dlq defaults to true, but such
a domain resolves to the event_store subscription type, and the engine turns
enable_dlq off for that type.
Common Options
| Option | Type | Default | Description |
|---|---|---|---|
subscription_type |
string | "event_store" |
Subscription type: "stream" or "event_store" |
messages_per_tick |
int | 100 | Messages to process per batch |
tick_interval |
int | 0 | Seconds between polling cycles |
StreamSubscription Options
| Option | Type | Default | Description |
|---|---|---|---|
blocking_timeout_ms |
int | 100 | Longest wait of an idle blocking read, in milliseconds. Must be positive. The Redis broker waits at most 1000 ms per read. A read returns as soon as a message arrives, so the value does not affect latency. With priority lanes on, the Redis broker waits on the primary and the backfill stream at once, and a message on either one ends the wait |
max_retries |
int | 3 | Retry attempts before moving to DLQ |
retry_delay_seconds |
int | 1 | Delay between retries |
enable_dlq |
bool | true | Enable dead letter queue. Always off for an event_store subscription |
dlq_retention_hours |
int | None | inherit | Override [server.dlq] retention_hours (default 168) for this subscription's DLQ stream only |
dlq_alert_threshold |
int | None | inherit | Override [server.dlq] alert_threshold (default 100) for this subscription's DLQ stream only |
circuit_breaker_threshold |
int | 10 | Consecutive handler failures that trip the circuit breaker OPEN |
circuit_breaker_reset_seconds |
float | 60 | Seconds an OPEN breaker waits before a single HALF_OPEN probe. Must be > 0 and finite (inf/nan are rejected) |
Known issue: per-handler DLQ overrides have no effect yet
Protean builds the subscription config for a handler without
dlq_retention_hours or dlq_alert_threshold. The engine therefore
applies the [server.dlq] values to every handler.
See Server Hardening → Circuit breaker for the state machine, the metric, and the trace events. See Tuning subscriptions for how to choose values for the subscription options on this page.
EventStoreSubscription Options
| Option | Type | Default | Description |
|---|---|---|---|
position_update_interval |
int | 10 | Messages between position writes |
origin_stream |
string | None | Filter by origin stream category |
One more setting applies to every event-store subscription in the domain. Set
it under [server.event_store_subscription] in domain.toml. A handler or a
profile cannot override it.
| Option | Type | Default | Description |
|---|---|---|---|
max_tick_interval |
float | 1 | Longest wait, in seconds, between reads while a subscription finds no new messages. With tick_interval 0 the wait starts at 10 ms; otherwise it doubles from tick_interval. Each further empty read doubles it again, up to this value. A read that returns messages sets the wait back to tick_interval. 0 keeps the wait at tick_interval. A value above 0 and below tick_interval is ignored with a subscription.max_tick_interval_ignored warning. When the domain does not set it and tick_interval is above 1, the wait stays at tick_interval. A value that is not a finite number of 0 or more is ignored with a subscription.max_tick_interval_invalid warning, and the default applies |
Configuration Methods
1. Handler Decorator Options
The examples on this page use this model:
@domain.aggregate
class Order:
total: Float()
@domain.event(part_of=Order)
class OrderCreated:
order_id: Identifier(required=True)
@domain.event(part_of=Order)
class OrderShipped:
order_id: Identifier(required=True)
@domain.aggregate
class Notification:
message: String()
@domain.aggregate
class Inventory:
sku: String()
@domain.projection
class OrderSummary:
order_id: Identifier(identifier=True)
total: Float()
Configure directly on the handler decorator:
@domain.event_handler(
part_of=Order,
subscription_type="stream",
subscription_profile="production",
subscription_config={
"messages_per_tick": 50,
"enable_dlq": True,
},
)
class OrderEventHandler:
@handle(OrderCreated)
def handle_order_created(self, event): ...
The options combine with priority:
subscription_configvalues override everythingsubscription_typesets the subscription type, even when the profile names another typesubscription_profileprovides base defaults
2. Domain Configuration File
Configure in domain.toml:
[server]
# Default settings for all handlers
default_subscription_type = "stream"
default_subscription_profile = "production"
messages_per_tick = 100
# StreamSubscription defaults
[server.stream_subscription]
blocking_timeout_ms = 5000
max_retries = 3
retry_delay_seconds = 1
enable_dlq = true
circuit_breaker_threshold = 10
circuit_breaker_reset_seconds = 60
# EventStoreSubscription defaults
[server.event_store_subscription]
position_update_interval = 10
max_retries = 3
retry_delay_seconds = 1
enable_recovery = true
recovery_interval_seconds = 30
gap_timeout_seconds = 5 # $all subscriptions: hold at a global_position gap this long before abandoning it
max_tick_interval = 1 # longest idle wait between reads; 0 turns the idle backoff off
# BrokerSubscription defaults
[server.broker_subscription]
max_retries = 3
retry_delay_seconds = 1
enable_dlq = true
# Version conflict auto-retry
[server.version_retry]
enabled = true
max_retries = 3
base_delay_seconds = 0.05
max_delay_seconds = 1.0
# Handler-specific overrides
[server.subscriptions.OrderEventHandler]
profile = "fast"
messages_per_tick = 50
[server.subscriptions.InventoryProjector]
subscription_type = "event_store"
profile = "projection"
3. Environment-Specific Configuration
Use environment sections for different deployment environments. Protean picks
the section that matches PROTEAN_ENV and merges it into the base [server]
settings:
# Development defaults
[server]
default_subscription_type = "event_store"
# Production overrides
[prod.server]
default_subscription_type = "stream"
messages_per_tick = 200
[prod.server.stream_subscription]
max_retries = 5
enable_dlq = true
# Staging overrides
[staging.server]
default_subscription_type = "stream"
messages_per_tick = 50
To use a different profile per environment, name it per handler under
[server.subscriptions.<Handler>] in the environment section.
default_subscription_profile has a lower priority than the server-level
defaults, so it sets few fields (see the caveat).
Configuration Examples
Example 1: Production Event Handler
A handler with production settings and custom batch size:
@domain.event_handler(
part_of=Order,
subscription_profile="production",
subscription_config={
"messages_per_tick": 200, # Higher throughput
},
)
class OrderEventHandler: ...
Resolved configuration:
{
"subscription_type": "stream", # From profile
"messages_per_tick": 200, # From subscription_config (override)
"blocking_timeout_ms": 5000, # From profile
"max_retries": 3, # From profile
"retry_delay_seconds": 1, # From profile
"enable_dlq": True, # From profile
}
Example 2: Low-Latency Handler
A handler optimized for fast processing:
@domain.event_handler(
part_of=Notification,
subscription_profile="fast",
)
class NotificationEventHandler: ...
Resolved configuration:
{
"subscription_type": "stream",
"messages_per_tick": 10,
"blocking_timeout_ms": 100, # Short timeout for quick response
"max_retries": 2,
"retry_delay_seconds": 0, # No delay between retries
"enable_dlq": True,
}
Example 3: Projection Handler
A projector using EventStoreSubscription:
@domain.projector(
projector_for=OrderSummary,
aggregates=[Order, Inventory],
subscription_profile="projection",
)
class OrderSummaryProjector: ...
Resolved configuration:
{
"subscription_type": "event_store", # Direct event store access
"messages_per_tick": 100,
"tick_interval": 0,
"position_update_interval": 10,
"enable_dlq": False, # Not supported
}
Example 4: Mixed Configuration
Combining domain config with handler config. The handler's server config names
the profile, so the profile takes priority over the server-level defaults (see
the caveat on default_subscription_profile):
# domain.toml
[server.subscriptions.SpecialHandler]
profile = "production"
messages_per_tick = 500
max_retries = 10
@domain.event_handler(
part_of=Order,
subscription_config={
"enable_dlq": False, # Highest priority - overrides everything
},
)
class SpecialHandler: ...
Resolved configuration:
{
"subscription_type": "stream", # From profile (via server.subscriptions.SpecialHandler)
"messages_per_tick": 500, # From server.subscriptions.SpecialHandler
"max_retries": 10, # From server.subscriptions.SpecialHandler
"enable_dlq": False, # From handler subscription_config (override)
"blocking_timeout_ms": 5000, # From profile
"retry_delay_seconds": 1, # From profile
}
Example 5: Origin Stream Filtering
Filter events by their origin stream:
@domain.event_handler(
part_of=Notification,
stream_category="order",
source_stream="user_actions", # Only events from user_actions origin
subscription_type="event_store",
)
class UserActionNotificationHandler:
"""Send notifications only for user-initiated order events."""
@handle(OrderShipped)
def notify_user(self, event):
# Only called for OrderShipped events that originated from user_actions
...
Configuration Validation
Protean validates configuration and provides helpful error messages:
Outbox / Subscription Type Conflict
Setting enable_outbox = true with default_subscription_type = "event_store"
is a configuration error. The outbox publishes events to Redis Streams, but
event-store subscriptions never read from them, resulting in a broken pipeline.
ConfigurationError: Configuration conflict: 'enable_outbox' is True but
'server.default_subscription_type' is 'event_store'. When outbox is enabled,
subscription type must be 'stream' so that subscriptions read from the broker
where the outbox publishes. Either set server.default_subscription_type = 'stream'
or remove enable_outbox.
Setting default_subscription_type = "stream" automatically enables the outbox
without needing to set enable_outbox at all.
Missing External Broker
Configuring an external_brokers entry that doesn't match a registered broker
is a startup error. This configuration is wrong on purpose:
nonexistent_broker is not a registered broker.
[outbox]
external_brokers = ["nonexistent_broker"]
ValueError: External broker 'nonexistent_broker' configured in
outbox.external_brokers but not found in domain broker configuration
Missing Outbox Table
When the outbox is on, every managed database provider needs its outbox table.
The server checks for it at startup and stops when it is missing. Starting the
server before running protean db setup or protean db setup-outbox gives:
ConfigurationError: Outbox table not found for provider 'default'. Run
'protean db setup' or 'protean db setup-outbox' to create it.
Published Events Without External Brokers
If your domain has events with published=True but no external_brokers
configured, Protean logs a warning during initialization:
WARNING: Domain has published events but no external_brokers configured
in outbox settings. Published events will only be dispatched internally.
This is informational, not an error. See Dispatching Published Events to External Brokers to configure external dispatch.
Invalid Subscription Type
# fragment
@domain.event_handler(subscription_type="invalid")
An unknown subscription type does not raise. The engine logs a warning when it
builds the handler's subscription and falls back to event_store:
WARNING: Unknown subscription type 'invalid', defaulting to EVENT_STORE
If a handler does not get the subscription you expect, check the log at startup.
Invalid Profile
# fragment
@domain.event_handler(subscription_profile="nonexistent")
The engine raises this error when it builds the handler's subscription:
ConfigurationError: Unknown subscription profile 'nonexistent'.
Valid profiles: batch, debug, fast, production, projection
Incompatible Options
# fragment
@domain.event_handler(
subscription_type="event_store",
subscription_config={"enable_dlq": True}
)
Protean logs no error here. enable_dlq controls the broker-side dead letter
stream, which only a stream subscription has. So the engine turns enable_dlq
off for an event store subscription and keeps the rest of the configuration. An
event store subscription records failed positions in its own failed-position
stream and retries them in a recovery pass, described in
Error Handling.
Configuration guidance
1. Use Profiles as a Starting Point
Start with a profile and override specific options:
# fragment
@domain.event_handler(
part_of=Order,
subscription_profile="production",
subscription_config={
"messages_per_tick": 50, # Adjust based on your workload
}
)
2. Configure at the Right Level
- Handler level: Handler-specific tuning
- Server config: Domain-wide defaults
- Environment config: Environment-specific overrides
3. Use debug Profile in Development
Name the debug profile on the handlers you are working on:
# domain.toml (development)
[server.subscriptions.OrderEventHandler]
profile = "debug"
default_subscription_profile = "debug" changes nothing. Protean's built-in
server-level defaults set the fields that the debug profile changes, and the
resolver applies those defaults after the profile, as the
caveat explains.
4. Monitor and Adjust
Start with conservative settings and adjust based on monitoring:
# fragment
# Start conservative
subscription_config={"messages_per_tick": 10}
# Increase after monitoring shows capacity
subscription_config={"messages_per_tick": 100}
5. Enable DLQ in Production
Always enable DLQ for production workloads to capture failed messages:
# fragment
subscription_config={"enable_dlq": True}
Related pages
- Subscription Types: Subscription types
- Running the Server: CLI options and deployment