67

What I understand is that all three concepts are related to long-running transactions.

A process manager is, to my understanding, a finite state machine which simply reacts on events and emits commands. It does not contain any business logic, it just does routing. Its goal is to bring you to a final state, where you know that your transaction has succeeded or failed.

So far, so good.

But now my problems in understand start:

  • What is a saga in contrast to a process manager?
  • There is also the document-based approach, as mentioned in CQRS sagas - did I understand them right? … as I understand it, a document is just a "piece of paper" where you take notes and hand it around. How does that fit into the concept of commands and events?

Can anybody please explain the differences, and - what I'd be especially interested in - which of these concepts is good for what, and when you do need what. Are they mutually exclusive? Can you go along all the way with only one of them? Are there scenarios where you need more than one? …?

Community
  • 1
  • 1
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 10
    In 1987, some guy published a paper, attaching his concept to the word "Saga". This short-sightedness forever took away anyone's capability to use the 4-character (therefore highly desirable) word as a moniker for any other similar concept. So now we all have to type longer words like ProcessManager or Workflow as we are programming. This is readily enforced by some people on the internet. – Kasey Speakman Nov 12 '13 at 16:07
  • Do you have any sources for this? – Golo Roden Nov 12 '13 at 19:47
  • 3
    While being mainly satirical, yes I do have a source. http://www.amundsen.com/downloads/sagas.pdf – Kasey Speakman Nov 12 '13 at 20:21
  • 2
    I prefer the term 'Policy' it is short, and is often congruent with business terms... not technical jargon. – Nescio Jul 28 '15 at 20:48

5 Answers5

62

What is a saga in contrast to a process manager?

The intent of these patterns is different. A process manager is a workflow pattern which can, as you say, be built on top of a state machine. A process manager will retain state between messages, and will contain logic in order to determine which action should be taken in response to a message (for example, transitioning state or sending another message). Some frameworks incorrectly refer to these as sagas.

By contrast, a saga (according to the original definitions) is a pattern intended to help manage failures. It involves multiple workflows across systems, where each will allow some form of compensating action to be taken in a later transaction in the case of a failure elsewhere.

This compensation is the defining characteristic of a saga. Note that the saga itself does't know what the compensating action might be. Sagas are often implemented using the routing slip pattern.

Are they mutually exclusive? Can you go along all the way with only one of them?

They aren't mutually exclusive - it's likely that, for example, a system participating in a saga might use a process manager to actually handle its piece of processing.

Other Resources

Some of these posts may help provide more detail and provide examples:

Roman Motyka
  • 649
  • 1
  • 6
  • 15
James Nugent
  • 766
  • 5
  • 3
  • These two patterns plus the retry pattern are often combined into the Scheduler Agent Supervisor pattern. More explanation: https://medium.com/@juan.castaneda.dominguez/cloud-design-patterns-in-microservices-architectures-part-3-2f5b86939758 – Christoph May 22 '19 at 13:47
  • I keep reading this difference and I can say for sure I am still confused because to me they reason out the same. So let me is ask, is a saga= movie and process manager= actor? – kimathie Apr 29 '21 at 12:19
  • None of the "Other Resources" links work today (March 2022). They all 404. – ssmith Mar 04 '22 at 16:42
31

Take a look at CQRS Journey project on MSDN:

http://msdn.microsoft.com/en-us/library/jj591569.aspx

Clarifying the terminology

The term saga is commonly used in discussions of CQRS to refer to a piece of code that coordinates and routes messages between bounded contexts and aggregates. However, for the purposes of this guidance we prefer to use the term process manager to refer to this type of code artifact. There are two reasons for this:

There is a well-known, pre-existing definition of the term saga that has a different meaning from the one generally understood in relation to CQRS. The term process manager is a better description of the role performed by this type of code artifact.

Although the term saga is often used in the context of the CQRS pattern, it has a pre-existing definition. We have chosen to use the term process manager in this guidance to avoid confusion with this pre-existing definition.

The term saga, in relation to distributed systems, was originally defined in the paper "Sagas" by Hector Garcia-Molina and Kenneth Salem. This paper proposes a mechanism that it calls a saga as an alternative to using a distributed transaction for managing a long-running business process. The paper recognizes that business processes are often comprised of multiple steps, each of which involves a transaction, and that overall consistency can be achieved by grouping these individual transactions into a distributed transaction. However, in long-running business processes, using distributed transactions can impact on the performance and concurrency of the system because of the locks that must be held for the duration of the distributed transaction.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 1
    Thanks for replying that fast. I already knew this explanation, but at least for me it does not explain what sagas are, only what their intent is - and to be true, I can not see the difference to the intent of a process manager. What's the difference in the concept? Is it simply two sides of the same coin? Can you exchange one with the other? Or are there scenarios where you need exactly one or the other? – Golo Roden Mar 20 '13 at 15:42
  • 7
    I understand that Process Manager is a new term for Saga that was introduced to limit confusion with a different meaning of the term Saga in distributed transactions world. So Process manager *is* Saga in CQRS. – Jakub Konecki Mar 20 '13 at 15:44
  • Does saga have state? – Matrix12 Sep 08 '18 at 07:19
  • Yes, it does. It needs to track what actions took place and what their result was, etc. – Jakub Konecki Sep 08 '18 at 07:21
7

Saga has no state while Process Manager has.

Another difference - Process Manager is a state machine, Saga is not.

Saga does not have

  • state machine state
  • data state (some persisted data)

.. and Process Manager has

  • state machine state
  • data state (some persisted data)

Read more in my blog: http://blog.devarchive.net/2015/11/saga-vs-process-manager.html

jgauffin
  • 99,844
  • 45
  • 235
  • 372
7

Saga and Process Manager are two integration patterns. They are very alike, but not in total.

  • Saga is a pattern that helps you to implement each business transaction that spans multiple services as a saga. In fact, you will create a sequence of local transactions where each local transactions updates the database and publishes a message or an event to trigger the next local transaction in the saga. There are two possible ways to implement a saga: Orchestration (an orchestrator tells the participants what local transactions to execute) and Choreography (each local transaction publishes domain events that trigger local transactions in other services). Is very common that using Saga will determine the usage of CQRS and EventSourcing.
  • Process Manager is a processing unit that it's existing in order to mantain the state of the sequence and determine the next processing step based on intermediate results. It's a routing pattern. It's more like an Orchestrator Saga.
Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
3

Both Process manager and saga route a message through multiple processing steps when the required steps may not be known at design time and may not be sequential.

Process manager pattern is a durable event scheduler that encapsulates process specific logic and maintain a central point of control deciding what to execute next once a process is completed. Process managers maintain state so for example say a payment was taken from a customer, the fact an order must now be sent to them is persisted in the process manager.

Saga manager pattern. Encapsulates process logic deciding what to execute next once a process is completed. A saga holds no state so decideds what to do next based entirely on the content of the incoming message or event. So in the case when a payment is taken by a process, that process also creates a new message indicating an order must now be sent, including what needs to be sent and to whom. The message also contains additional payment information so if something were to go wrong sending the order the payment gets refunded.

andrew pate
  • 3,833
  • 36
  • 28