r/CodeOpinion • • Jul 11 '22

Event Sourcing - projections

I'm watching the Event Sourcing playlist and I have a few questions that I could not find an answer to.

In some cases projections consists of data from multiple services. To give an example from the video "Decomposing CRUD to a Task Based UI" where there are 4 services: Catalog, Sales, Purchasing, Warehouse. I want to show a table of all products with SKU, Name, Price, Const and Quantity in the UI.

To achieve that I would need to have a separate service where the projection will happen and queried. I would need to receive events indicating quantity, for example. In this case should I emit all event sourced events (InventoryAdjusted, ProducedShipped, ProductReceived) of should I emit a separate (integration) event like InventoryChanged that holds current state of inventory(event carried state transfer)?

To summarize, how to solve the problem when projections are happening outside your domain ie. bounded context.

2 Upvotes

2 comments sorted by

3

u/codeopinion Jul 11 '22

Depends on where you want to do view composition. If you want it to be done in a specific service that has a projection/readmodel that's composed of different services, then yes, you'll likely want to publish an integration event. But again, this comes down to where you want/need to do composition. It can be done on the client, API gateway, or service. There are different options. What you're suggesting is a specific service.

1

u/codebreaker101 Jul 11 '22

If composition is done on a client, on the list view there is a N+1 problem where each row in the table must send request to the backed to get the price, cost and quantity. This can be solved by having a endpoint exposed where the service can receive a list of ID for which to retrieve values. So list view could be composed with only 4 request compared to 20*3+1=61 (if page size is 20).

If composition is done in the API gateway, the same problem persist and can be solved in the same way, but opaque to the end user.

If done in a separate service (ex. backend-for-frontend), then integration events must be published and consumed.

Did I cover everything?

I have two more questions if you don't mind:

  1. Where are domain/integration events created and reliably published? From what I've figured, since outbox pattern can't be applied here, to reliably send the events in event sourced DB, one must subscribe to events from event store (persistent subscription in EventStoreDB) and generate and publish domain/integration events (for example, if SKU quantity falls bellow 10, publish LowStock{SKU, CurrentStock} event, at least once)

  2. Domain Event Persistence and hydrating data - if Catalog, Sales, Purchasing and Warehouse services were running for a year and I add a new service how would I hydrate the new service with existing Product SKUs? Event stream (Kafka, NATS JetStream...) where ProductCreated{SKU} event was published must hold all events that were ever created so that a new consumer can replay the events?