Stream exists?

Is there a performant way to detect whether a stream exists or not?

Justification: I’m migrating code from an existing CQRS/ES implementation with home-grown event store to GetEventStore and to minimize refactoring I need this functionality…

Sorry, my finger was too quick… found it myself. The status of the ReadStreamEventsForward/Backward tells me exactly what I want

I’m not sure if this is the recommended way to check if a stream exists, but it does work. :wink:

var slice = connection.ReadStreamEventsForward(streamName, 0, 1, false);

if (slice.Status == SliceReadStatus.StreamNotFound){

…yada yada


what are your consistency requirements?

Internally we also have single event read (with ability to read latest event in stream, whatever that event number is) operation, which is the fastest among all read operations.
Maybe it will be helpful to expose that in ClientAPI? That will simplify and make faster your check for stream existence.

But your variant is perfectly fine, btw.

well, I know that it is not the best way to check whether a stream exist but to analyze what command I get and if it is a creational command to assume the stream does not exist.
The latter I do in new code but I have some legacy code to convert and I want to do it with the least pain possible…

The legacy aggregate factory just blindly tried to load a stream from the store and re-hydrate the aggregate and if it didn’t exist it would just create a new aggregate…

My legacy code did the same.

Now I just let the the stream get created implicitly via Repository.Save(aggregate). That may or may not fit your needs.

I only need to check for a stream when trying to read a snapshot (stream with metadata{$maxCount:1}). Because that stream is “special”, it turns out that’s perfect place to create the stream if it does exist.

does not exists.

No sure if I’m understanding the problem correctly here - on writing, optimistic concurrency should take care of new aggregates and whether or not their stream already exists?

I think you want to look at ExpectedVersion enum in client API. One of the options is to say “I expect this stream does not exist” when doing a write. If you do read/write you still have a race condition that needs to be handled.

This (relatively newly written) doc page explains the options:

Thanks for all the explanations and the new documentation.
What I needed was a tolerant GetById repository function which would not throw if the stream for the according id does not yet exist but rather act like a factory and return a new instance of the corresponding aggregate type.

I use the code found here but rather than throwing an AggregateNotFoundException I just return a new fresh instance of the aggregate.