vendredi 9 février 2018

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 :

  • create an appointment AppointmentCreatedEvent
  • reschedule the appointment AppointmentRescheduledEvent
  • And then, cancel the appointment AppointmentCancelledEvent

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?

Aucun commentaire:

Enregistrer un commentaire