orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Reducing time between checkpoints with ConfigureCacheEviction

Open cjenp opened this issue 5 years ago • 6 comments

Hi,

our solution is using an event hub stream provider to receive data from our IoT devices (Azure IoT Hub). We noticed that event thought our checkpointer persist interval is set to a few seconds, the checkpoint does not get updated. After some searching I have found that the checkpoint is updated when events are purged from the message cache (#6581).

Our problem is as follows: we are receiving messages from devices via IoT Hubs build-in event hub (at most 4 per minute). If we shutdown and restart our silo host, the messages from the stream get replayed from the latest checkpoint (if I understand correctly this can be up to 30min in the past with the default settings). Since our grains are idempotent, each replayed message that was already processed by the grain throws an exception. The stream then also retries the duplicate messages a few times. This causes our grains to fall behind, and they need a few minutes after silo restart to start processing the messages received in real-time.

To solve this we would like to adjust the cache eviction options to happen at least once per minute. Would this cause any issues with the stream or affect its performance? Our thinking is, that since once the messages from the stream are successfully processed we don’t need them anymore, so there is no point in keeping them in the cache for that long.

Also, is it possible to somehow configure or disable retrying of failed messaged received from the stream?

cjenp avatar Jul 07 '21 07:07 cjenp

We have a similar issue and I am unable to determine the best way to solve the problem without changing the consumer system. Our stream consumers are stateless. They get the event and forward it on to other external services. If the silos are restarted, they receive the last 30 minutes worth of messages all over again. We basically want a configuration which tells the streams, on silo restart, to resume from the latest message instead of what was in cache.

bobbyalex avatar Jul 13 '21 06:07 bobbyalex

Sorry for the delay, I was OOF.

The Event Hub streaming provider is an at-least-once delivery, I strongly suggest to make your grains capable of handling duplicate messages.

To solve this we would like to adjust the cache eviction options to happen at least once per minute. Would this cause any issues with the stream or affect its performance?

It should be fine, but there will be several options to setup to make it work properly. I will check my notes on that.

Also, is it possible to somehow configure or disable retrying of failed messaged received from the stream?

No, and that would be dangerous do to that IMO.

We basically want a configuration which tells the streams, on silo restart, to resume from the latest message instead of what was in cache.

You will lose messages if you do that. If you really really want that, you could maybe inject a no-op checkpointer. If there is no checkpoint available and you set EventHubReceiverOptions.StartFromNow to true, it will only start from the latest message.

I know that you both said that your grains are stateless, but storing the streaming token in the grain would solve your issues. Could you maybe store it in a redis temporary storage?

benjaminpetit avatar Jul 30 '21 10:07 benjaminpetit

@benjaminpetit I have changed our implict streams consumers to store the token and pass it back when SubscribeAsync is called. In spite of this, the messages are being redelivered. The default value of StartFromNow is true by default. It doesn't seem to have an impact. Messages are still redelivered.

 public bool StartFromNow { get; set; } = DEFAULT_START_FROM_NOW;
 public const bool DEFAULT_START_FROM_NOW = true;

I have also set the checkpointer PersistInterval configuration property to 5 seconds.

bobbyalex avatar Jul 30 '21 12:07 bobbyalex

Documentation of EventHubReceiverOptions.StartFromNow:

In cases where no checkpoint is found, this indicates if service should read from the most recent data, or from the beginning of a partition.

This setting is only used if there is no checkpoint in storage. You would have to inject your own no-op checkpointer, instead of the Azure Table one, that should be the one you are using.

Can you share the code where you subscribe again?

benjaminpetit avatar Jul 30 '21 13:07 benjaminpetit

public override async Task OnActivateAsync()
{
     //.....
      var handle = await inputStream.SubscribeAsync(ReceiveCommand, OnCommandError, 
      OnCommandCompleted,_streamState?.State.Token);
      //....
}

and we are persisting the StreamSequenceToken everytime ReceiveCommand is called.

bobbyalex avatar Aug 02 '21 11:08 bobbyalex

Thank you for your help @benjaminpetit , we followed your advice and changes the grains so that they ignore duplicate messages.

cjenp avatar Aug 26 '21 12:08 cjenp