grpcevent-sourcingeventstoredbevent-store

EventStore batch event reading from $all via gRPC


An application is written on C#, is trying to use EventStore.Client.Grpc.Streams version 23.1.0 to access EventStore.

var positionToRead = startFromPosition == 0 ? Position.Start : new Position(startFromPosition, startFromPosition);
var readStreamAsync = _client.ReadAllAsync(Direction.Forwards, positionToRead, take);

When the last event is received, the application knows its Position. let's imagine it's 777. After that application restarted, when it was restarting, another event was written to EventStore, let's imagine it's position 1111.

How to continue reading from the new (1111) event?

If you try to use position 778 during ReadAllAsync (expecting it indicates your goal to read events after 777)- the application throws an exception, I guess it's because the next event position is 1111 and you need to know the precise position 1111.

How to read event A from $all, after certain event B, if you don't know the position of A, but you know that position B is higher than position A ?


Solution

  • In order to read from $all, you need to use a valid commit position. You can't use 778 because commit position is not a sequential number of an event in $all, it's more like an offset in the global log. It's confusing, but that's how it is.

    Reading from $all is not a common as you'd normally subscribe to $all to receive events as they are appended to the database. Subscription to $all starts from the next event after the start position you provide. In addition, subscriptions allow you to filter out events on the server, like system events. When reading from $all, you will get system events alongside with the actual data events.

    If you, for some reason, need to read from $all, I can suggest that if the start position is not Position.Start, you deliberately skip one event when iterating through the async enumerable.