Possible message loss with OverrideTimeToBeReceived/TimeToBeReceivedOnForwardedMessages on transactional MSMQ endpoints.
Taskforce: @janovesk @szymonpobiega
Who's affected
This bug affects you if you are using the MSMQ transport, have a transactional endpoint and have OverrideTimeToBeReceived defined in the AuditConfig section of your config file. You are also affected if you are using ForwardReceivedMessagesTo combined with TimeToBeReceivedOnForwardedMessages in your UnicastConfig config section.
Symptoms
Audit/forwarded messages do not get the configured TimeToBeReceived(TTBR) set. They are assigned a seemingly random TTBR if any. If the TTBR is set lower than the configured OverrideTimeToBeReceived, the messages will be purged from the audit/forwarded queue prematurely. If set higher, they will be purged late. If no TTBR is set, the messages stay in the audit/forwarded queue indefinitely.
Background
We offer OverrideTimeToBeReceived to override the TimeToBeReceived for audit messages. The use case is customers who want to keep their audit log for a period and then have the messaging infrastructure remove it. Audit messages not timely removed can eventually cause problems with queue size and disk space limitations.
The same applies for the TimeToBeReceivedOnForwardedMessages config avaialble for forwarded messages.
Root cause
Unfortunately, MSMQ does not support having differing TTBR when you send multiple messages in the same transaction. All the messages get the TTBR of the first message sent. NServiceBus implements auditing and forwarding by sending a copy of the original message to the configured audit/forward queue. It will join in on any ambient or native transaction active on the endpoint. The audit/forward message will get the TTBR set on the first message sent by users in their message handler, instead of the one configured override.
Further discussion in https://github.com/Particular/NServiceBus/issues/2886.
Workarounds
- Stop using OverrideTimeToBeReceived for your audits and forwards. This has the downside of you having to put in place a different mechanism to make sure the audit/forward queue does not fill up. The recommended option is to use SC/SP to consume the audit queue and lean on it's built-in feature for auto-removal of old data. Another option is to create a script/tool to periodically clean old messages from the queues.
Affected versions
Version 4+
Plan of attack
- [x] Modify V4 core to throw on endpoint startup with the combination of MSMQ with transactions and a configured
OverrideTimeToBeReceived/TimeToBeReceivedOnForwardedMessages - [x] Modify V5 core to throw on endpoint startup with the combination of MSMQ with transactions and a configured
OverrideTimeToBeReceived/TimeToBeReceivedOnForwardedMessages - [x] Modify V6 core to throw on endpoint startup with the combination of MSMQ with transactions and a configured
OverrideTimeToBeReceived/TimeToBeReceivedOnForwardedMessages - [x] Docu this caveat.
- [x] Docu how the audit queue works on different transports. MSMQ, RabbitMQ, SQL Transport and Azure all handle expired messages differently and thus gives different types of guarantees with regards to not filling up queues and running into disk space limitiations.
Impact assessment
Customer must be using both MSMQ, run with transactions and have OverrideTimeToBeReceived set. The first is very common; the latter presumed to be fairly uncommon. We do have confirmed customer reports on this, but I suspect this is also happening to customers without them knowing. It can be hard to notice that messages are removed prematurely or late from the audit queue. Given the lack of complaints, this actual number of affected customers can't be very high. That said, when it occurs, it is effectively message loss. Therefore, I say impact medium. Size is only a couple edits to put the deprecation and a warning in place. It should be fairly quick, I'd say size small.
Customer communication
We have discovered a bug in the OverrideTimeToBeReceived feature. It lets you specify a TimeToBeReceived for your audit messages. If you use this in combination with the MSMQ transport and have transactions turned on, your audit messages might get the incorrect TimeToBeReceived set. The messages might get removed from your audit queue prematurely or build up over time. The later can cause issues because of queue size and disc space limitations.
The same issue is unfortunately also present if you are using forwarding with ForwardReceivedMessagesTo and have a TimeToBeReceivedOnForwardedMessages set.
Unfortunately, we have no other choice than to disable this piece of functionality when running the MSMQ transport with transactions on because of underlying issues in MSMQ.
This means you have to
- Stop using OverrideTimeToBeReceived for your audit messages. This has the downside of you having to put in place a different mechanism to make sure the audit queue does not fill up. The recommended option is to use SC/SP to consume the audit queue and lean on it's built-in feature for auto-removal of old data. Another option is to create a script/tool to clean periodically old messages from the audit queue.
- Stop using TimeToBeReceivedOnForwardedMessages if you use the message forwarding feature.
I've done some more digging.
The intention of this feature was to make sure we don't run into disk space limitations for the audit queue by having the underlying transport remove timed out messages and release any disk space they use.
For our supported transport, this does not work reliably for ANY of them.
- SQL Transport will only evaluate the TTBR when/if the message is received from the audit queue. This will not happen for an audit queue unless you are actively receiving all the messages in it, in which case the TTBR override is meaningless.
- Azure acts the same way as the SQL Transport. TTBR is evaluated only at receive time.
- RabbitMQ evaluates the TTBR ONLY for the message in the front of the queue. Meaning that if you have a single message in the audit queue with non-small TTBR, no other messages will be cleared out because of THEIR elapsed TTBR until the front message is removed.
- Msmq is the only transport that evaluates the TTBR of all messages in the queue continuously. Here the problem is that we can not support the feature on any transactional endpoint because of the "only a single TTBR in a transaction" limitation described earlier. Msmq without transactional wrapping, either by turning off transactions explicitly or using the outbox, will work.
With this feature only working as intended for MSMQ without transactions I suggest we drop it. We can't deliver it reliably.
@SzymonPobiega, @andreasohlund and @udidahan can we agree that it's time for this feature to go when it basically does not work reliably on any transport?
Is there anything besides guidance that we can provide instead of this feature?
:+1: for removing
Updated the issue to reflect that this is both for audits and forwarding.
We could provide sample scripts that clear messages older than X for all the different transports. Customers could use something like that in a scheduled task to get the same disk space saving effect. I guess that would fall under guidance though. @andreasohlund
We could provide sample scripts that clear messages older than X for all the different transports. Customers could use something like that in a scheduled task to get the same disk space saving effect. I guess that would fall under guidance though.
That was what I was after, :+1:
As this qualifies as a bug, it doesn't require my approval. If you feel that you're ready to move forwards, go ahead.
@janovesk What's the status on this?
Code is ready, customer communications is ready. V4 and V5 releases go out today.