Accessing a database or webservice in a saga

Hi, I’m reading Sagas • NServiceBus • Particular Docs and I’m wondering why a saga shouldn’t access a database or do a webservice call? What is the motivation? What can / will go wrong?

Thanks,
Peter

Hey Peter,

sagas are meant to be coordinators of processes, not implementations of whole processes. Every single step, like doing a service call, or processing some data should be addressed using a regular handler. A single saga instance, should obtain all the data needed to proceed from its inputs (messages) and communicate with the rest of the world with messages as well. It’s a powerful coordinator, but still, just a coordinator.

Does it answer your question?

Cheers,
Szymon

Yes, thank you.

Peter

1 Like

@Scooletz , but what if message does not contain enough data? Is it still worth creating a reply-response flow with an additional handler? Why not simply query DB directly in saga’s message handler?

Message A → query additional data from DB and augment saga with that data.
Message B → use saga data.

@voroninp there is nothing preventing that, you can have a saga query that DB. The thing is more conceptual and design-related. The goal is to keep components as autonomous as possible. If the saga knows where to query data that the sending component owns it might make them more coupled than needed.

Sent to early, sorry. Another thing to keep in mind is the infrastructure requirements that this might imply. If the saga needs to access another database other than the one it already uses to saga data, it might cause technical issues in the long run. It’ll be easy for a developer to say: well, I’m reading from that database, let me quickly write too.

I agree with your arguments. But I saw our sagas send commands to handlers (same process) which query data and then send it back to saga. This is quite a weird design. It gives nothing but adds to complexity.

If saga can be self-sufficient by acquiring all data from events and commands, that’s indeed nice.

In that case, it sounds like a waste.

Can you describe the scenario in detail?