Vivendi
Vivendi

Reputation: 21057

When to read from Eventstore database

I'm looking into Event Sourcing and I think I understand most of it. Obviously an event store database isn't optimal for querying data. So that's where the Read only database jumps in, aka projections.

What I would do is update a denormalized database accordingly after an event has occurred.

Now suppose I have a system where a User can Schedule an Appointment. So a user can :

In my read database I would only have one record with the current state of the Appointment, that is Cancelled.


Here comes my question about this.

Suppose I'm only ever interested to query appointments by their state. So, either active or cancelled.

Should I then only create a read database tabel containing only that much information?

Appointments table (read only database)

--------------------------------------------------------------
| Id (same as AggregateId in EventStore db)   | State        |
--------------------------------------------------------------
| 1001                                        | Cancelled    |
| 1002                                        | Active       |
| 1003                                        | Active       |
--------------------------------------------------------------

So if I'm interested in all cancelled appointments I would then first query the read database

Example: SELECT Id FROM Appointments WHERE State = 'Cancelled'

And then use the Id of every Cancelled record to query the EventStore database to rebuild the state of the actual AggregateRoot object.

Ultimately I want to show all the information that the AggregateRoot Appointment object has.

Or...

Should I enrich my read database with as much data as possible, thus never having to go to the EventStore database?

Which of the two is the best approach in an Event Sourced system?

Upvotes: 1

Views: 581

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

Ultimately I want to show all the information that the AggregateRoot Appointment object has.

Why would you want that? In Event sourcing the Aggregate is used only to process commands; in other words you don't query it nor you depend on its internal/private state.

If you need some state, with some structure, you build a Read model just for that.

Should I enrich my read database with as much data as possible, thus never having to go to the EventStore database?

Yes. The Event store is used to rehydrate an Aggregate before executing commands and to completely rebuild a Read model (if you need to).

Upvotes: 2

Related Questions