Strategy for handling poison messages and retries in stream processing
Hi,
Is there any default way to handle poison messages in streamsubscribers. We're facing an issue, related to https://github.com/dotnet/orleans/issues/2983 (I think) and would like to apply a poison message handling strategy, e.g. retry delivery of a failing message X times with backoff strategy, after that store the message in place where an operator can optionally fix and retry the message. Much like the deadletter queue in azure servicebus or the fault handling in libraries as NServiceBus and MassTransit.
We're using the Azure Queue provider at the moment, but maybe an other provider is more suiteable?
I'll let @jason-bragg or @xiazen provide the details, but there's a way to install an application handler that will be invoked when a stream message fails to deliver N times in a row. I can't find this info in the docs. So we likely need to document the feature.
Hi,
Any update on this? Sorry for pushing, but I'm supposed to come up with a solution for this by the end of this week :)
The application handler is IStreamFailureHandler, you can register your own through customizing AzureQueueAdapterFactory.StreamFailureHandlerFactory
as follows is its interface
public interface IStreamFailureHandler
{
bool ShouldFaultSubsriptionOnError { get; }
/// <summary>
/// Should be called once all measures to deliver an event to a consumer have been exhausted.
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="streamProviderName"></param>
/// <param name="streamIdentity"></param>
/// <param name="sequenceToken"></param>
/// <returns></returns>
Task OnDeliveryFailure(GuidId subscriptionId, string streamProviderName, IStreamIdentity streamIdentity, StreamSequenceToken sequenceToken);
/// <summary>
/// Should be called when establishing a subsription failed.
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="streamProviderName"></param>
/// <param name="streamIdentity"></param>
/// <param name="sequenceToken"></param>
/// <returns></returns>
Task OnSubscriptionFailure(GuidId subscriptionId, string streamProviderName, IStreamIdentity streamIdentity, StreamSequenceToken sequenceToken);
}
So with this , you can definite do store poison message after delivery failure, using its OnDeliveryFailure method.
EDIT: sorry I wasn't paying attention to that OnDeliveryFailure does take the failed message as input, so Jason is right, you cannot build a dead letter queue using OnDeliveryFailure
But as to the logic of stop delivery after x times of failure, I haven't thought of an elegant way to do that, since the retry logic is INFINITE_RETRIES with backoff strategy, and this logic is not customizable yet.
maybe @jason-bragg can work some magic out of this. I will keep thinking though.
Ah I think I got something for you. There's a MaxEventDeliveryTime you can configure from PersistentStreamProviderConfig. This is the max time pulling agent will try to deliver a batch of event. It is not based on failure count yet, but I think it is a good alternative.
@rikbosch We don't have built in support for poison messages. We attempt to deliver the event to a consumer using an exponential back-off retry policy which retries for the configured MaxEventDeliveryTime period, then give up and call the OnDeliveryFailure call in the IStreamFailureHandler. For azure queue though that won't help much because the event will reappear in the queue if it was not delivered, so the notification only really notifies the application layer that this is happening, and the handler is not passed the message so it can't even be used to add the message to some dead letter queue.
Some users have handled this at the application layer. If the application layer tracks repeated errors on the same event it can choose to put that event into a dead letter queue and not throw the error back to the streaming system (hence breaking the redelivery cycle). This can be difficult with azure queues because it doesn't have a means of uniquely identifying a message, so the message itself must have some means of doing so, and azure queue messages must be processed in a timely manner or they will reappear in the queue, regardless of what the application layer does.
Thank you for your responses.
sorry I wasn't paying attention to that OnDeliveryFailure does take the failed message as input, so Jason is right, you cannot build a dead letter queue using OnDeliveryFailure
Since we have an eventsourced backend, we could leverage the StreamSequenceToken for this? We could have this token contain the global position of the event in our event store.
For azure queue though that won't help much because the event will reappear in the queue if it was not delivered
Is there another provider we can use? Like EventHubs? where this behavior is different?
If the application layer tracks repeated errors on the same event it can choose to put that event into a dead letter queue and not throw the error back to the streaming system (hence breaking the redelivery cycle)
What if there is a serialisation error, these can not be handled at the application layer right?
Since we have an eventsourced backend, we could leverage the StreamSequenceToken for this? We could have this token contain the global position of the event in our event store.
Yes I think we might be able to leverage StreamSequenceToken on this. But you might need to use your custom token, since the default token implementation is not unique to each message globally, it is only unique per stream provider per queue. And also AzureQueueStreamProvider doesn't support producer side giving a sequenceToken per message ASAIK.
The idea in my mind is customizing AzureQueueDataAdapter.FromCloudQueueMessage method. SequenceToken is generated in this method, so you can find the relation between CloudQueueMessage
msg and its SequenceToken here, and do some database logic here.
For azure queue though that won't help much because the event will reappear in the queue if it was not delivered
Is there another provider we can use? Like EventHubs? where this behavior is different?
I think you don't need to choose another stream provider, you can just simply delete the message from azure queue when the message failed delivery, using IStreamFailureHandler.onDeliveryFailure. You can find the relation between sequenceToken and the related CloudQueueMessage, using the method I mentioned above. This will prevent the message reappear in the queue
As to the difference of EventHubStreamProvider, it maintain ordered event, while AzureQueueStreamProvider doesn't.
sorry for the spam of comments, my browser is not quite working
@rikbosch, Some of your responses left me with the impression that I may have a misunderstanding of what you're trying to do. Questions below.
We could have this token contain the global position of the event in our event store.
Unclear what you mean here. Are you using azure queue stream provider or do you have your own custom adapter? If azure queue, how are you providing your own stream sequence token? Custom IAzureQueueDataAdapter?
The default sequence token provided by azure queue stream provider is not viable for uniqueness, but EventHubs is.
Is there another provider we can use? Like EventHubs?
EventHub stream provider is intended for ordered reliable delivery, but this comes at a cost of memory and complexity. We've more services using this in production, so it's been hardened more under load than some other providers. It's also more customizable, but again, all of this comes at a cost.
What if there is a serialisation error, these can not be handled at the application layer right?
Serialization errors can be a bit tricky. We don't have a good solution to them. We've an extensibility point in the azure queue stream provider (IAzureQueueDataAdapter) which allows applications to handle serialization (and any errors) as they see fit. You can provide a custom IAzureQueueDataAdapter and a custom IBatchContainer with your own serialization logic and return any serialization errors to the application layer as events in the IBatchContainer's GetEvents call.
For instance a custom IAzureQueueDataAdapter's FromCloudQueueMessage could return a custom IBatchContainer (which must be serializable) that has the raw bytes from the CloudQueueMessage. On the IBatchContainer's GetEvents
I apologize for duplicating some of what Xiao posted.. Didn't see her posts :/
yeah Orleans people are chatty :) . I think people generally get the impression. So no surprise.
Pretty chatty indeed :)
Unclear what you mean here. Are you using azure queue stream provider or do you have your own custom adapter? If azure queue, how are you providing your own stream sequence token? Custom IAzureQueueDataAdapter?
We are not (yet) using any custom adapters. Every event in our system is first appended in our eventstore and then published on a stream, which means we can identity each message in our eventstore by messageId or by global event position (ever increasing long).
Seeing that the OnNextAsync() method supports a StreamSequenceToken object, I thought I could leverage this, to add the EventId and GlobalPosition. Which we can then use in the IStreamFailureHandler implementation to retrieve the message from our eventstore, and add it to a deadletter queue for the subscription. Although this feels kinda hacky, it should work.
In the meantime I will study the existing AzureQueueDataAdapterV2
Seeing that the OnNextAsync() method supports a StreamSequenceToken object, I thought I could leverage this, to add the EventId and GlobalPosition. Which we can then use in the IStreamFailureHandler implementation to retrieve the message from our eventstore, and add it to a deadletter queue for the subscription.
OnNextAsync doesn't support assigning a StreamSequenceToken in the producer side, it only support that on the consumer side AFSIK. That's why I suggested customizing AzureQueueDataAdapterV2.
close this issue, solution provided and the issue owner doesn't seem to have further questions. Feel free to reopen if have more questions