DEV Community

Andrzej Krzywda
Andrzej Krzywda

Posted on

Querying data from process managers or sagas

Today I received such a question:

Hello! Is there ever a time where it’s appropriate to query the read model/projections from inside a saga/process manager? For example:
I have an application/inquiry form that can be submitted by leads, and sometimes they submit it more than once. I don’t want to create multiple records for a lead if I know I already have them (say by normalized email address). My initial thought was to create a saga around the application submitted event and optionally create the lead if they don’t already exist.

This is a question I often receive recently. Here is my answer:

In general, the answer is yes, sometimes it’s hard to avoid such “queries”.
However, there are some things I’d be careful with:

  • Try to avoid querying an exisitng read model that is also used for a UI purpose. This comes with a risk that a small change on the UI can result on breaking the process manager (in the worst case have a test which covers such integrations)
  • Watch out for too big projections/read-models - certain data (a list of all leads?) can be huge one day.
  • Choose the peristence method wisely - for bigger sets of data, projecting from events on the fly can be slow. In that case having a read model backed by a db table is OK
  • Sometimes having this need points to a potential design problem (the process manager being too big, similarly as with aggregates) - I don’t think this is your case, but just a word of warning

Discussion (0)