Exercise 16 · The whole process from approval to disbursement, compensation

Saga / Process Manager

  • Process Manager
  • orchestration vs choreography
  • kompenzace
  • timeout

Principle

Mortgage onboarding is a long process across several services: identity → account → contract → signature → disbursement. It cannot be wrapped in a single transaction.

The Process Manager (Saga) holds the process state, issues commands (…Requested) and reacts to results. Risk and Approval are choreography (services react to events), onboarding is orchestration.

When a step fails permanently, there is no “rollback”. The saga triggers compensation: it closes the account already opened. Without it the bank is left with an account and no payout.

What to watch

  • Process steps and their states in the Process Manager panel.
  • Process state: failed vs. compensated.
  • The MortgageAccountClosed event as a compensating action.

Experiment

What to do: Click “Run: payout is rejected, without compensation”. What to look at: The account stays open and the process ends in the failed state.

The simulation needs JavaScript. Below is a recording of both runs.

Run: payout is rejected, without compensation

Disbursement rejects the payout (limit/AML). The account and contract already exist and nobody cleans them up.

How the run unfolds (newest on top)

  1. 10.4 sProcess Manager (Saga)processedMortgageDisbursementFailedintegration

  2. 10.25 sProjection (read model)processedMortgageDisbursementFailedintegration

  3. 10.03 sProcess Manager (Saga)picked upMortgageDisbursementFailedintegration

    attempt 1

  4. 10.0 sProjection (read model)picked upMortgageDisbursementFailedintegration

    attempt 1

  5. 9.63 sDisbursementprocessedMortgageDisbursementRequestedcommand

  6. 9.63 sDisbursementappended and publishedMortgageDisbursementFailedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "reason": "Daily limit exceeded / AML check"
    }

    evt-0038 · correlationId calc-1 · causationId evt-0035

  7. 9.2 sProjection (read model)processedMortgageDisbursementRequestedcommand

  8. 8.95 sDisbursementpicked upMortgageDisbursementRequestedcommand

    attempt 1

  9. 8.88 sProjection (read model)picked upMortgageDisbursementRequestedcommand

    attempt 1

  10. 8.55 sProjection (read model)processedContractSignedintegration

  11. 8.5 sProcess Manager (Saga)processedContractSignedintegration

  12. 8.5 sProcess Manager (Saga)appended and publishedMortgageDisbursementRequestedcommand

    for: DisbursementProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "accountNumber": "0800-100017/0800",
      "customerId": "CUST-0001"
    }

    evt-0035 · correlationId calc-1 · causationId evt-0032

  13. 8.28 sProjection (read model)picked upContractSignedintegration

    attempt 1

  14. 8.23 sProcess Manager (Saga)picked upContractSignedintegration

    attempt 1

  15. 7.88 sDocument Serviceappended and publishedContractSignedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "contractId": "SML-0001",
      "signedBy": "klient"
    }

    evt-0032 · correlationId calc-1 · causationId evt-0029

  16. 7.03 sProjection (read model)processedContractGeneratedintegration

  17. 6.95 sProcess Manager (Saga)processedContractGeneratedintegration

  18. 6.68 sProjection (read model)picked upContractGeneratedintegration

    attempt 1

  19. 6.63 sProcess Manager (Saga)picked upContractGeneratedintegration

    attempt 1

  20. 6.35 sDocument ServiceprocessedContractGenerationRequestedcommand

  21. 6.35 sDocument Serviceappended and publishedContractGeneratedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "contractId": "SML-0001"
    }

    evt-0029 · correlationId calc-1 · causationId evt-0026

  22. 6.13 sProjection (read model)processedContractGenerationRequestedcommand

  23. 5.75 sProjection (read model)picked upContractGenerationRequestedcommand

    attempt 1

  24. 5.7 sDocument Servicepicked upContractGenerationRequestedcommand

    attempt 1

  25. 5.45 sProjection (read model)processedMortgageAccountOpenedintegration

  26. 5.33 sProcess Manager (Saga)processedMortgageAccountOpenedintegration

  27. 5.33 sProcess Manager (Saga)appended and publishedContractGenerationRequestedcommand

    for: Document ServiceProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "accountNumber": "0800-100017/0800",
      "customerId": "CUST-0001"
    }

    evt-0026 · correlationId calc-1 · causationId evt-0023

  28. 5.13 sProjection (read model)picked upMortgageAccountOpenedintegration

    attempt 1

  29. 4.98 sProcess Manager (Saga)picked upMortgageAccountOpenedintegration

    attempt 1

  30. 4.7 sMortgage AccountprocessedMortgageAccountOpeningRequestedcommand

  31. 4.7 sMortgage Accountappended and publishedMortgageAccountOpenedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "accountNumber": "0800-100017/0800"
    }

    evt-0023 · correlationId calc-1 · causationId evt-0020

  32. 4.45 sProjection (read model)processedMortgageAccountOpeningRequestedcommand

  33. 4.15 sMortgage Accountpicked upMortgageAccountOpeningRequestedcommand

    attempt 1

  34. 4.08 sProjection (read model)picked upMortgageAccountOpeningRequestedcommand

    attempt 1

  35. 3.88 sProjection (read model)processedCustomerIdentityCreatedintegration

  36. 3.75 sProcess Manager (Saga)processedCustomerIdentityCreatedintegration

  37. 3.75 sProcess Manager (Saga)appended and publishedMortgageAccountOpeningRequestedcommand

    for: Mortgage AccountProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "customerId": "CUST-0001"
    }

    evt-0020 · correlationId calc-1 · causationId evt-0017

  38. 3.58 sProjection (read model)picked upCustomerIdentityCreatedintegration

    attempt 1

  39. 3.45 sProcess Manager (Saga)picked upCustomerIdentityCreatedintegration

    attempt 1

  40. 3.18 sCustomer IdentityprocessedCustomerIdentityCreationRequestedcommand

  41. 3.18 sCustomer Identityappended and publishedCustomerIdentityCreatedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "customerId": "CUST-0001"
    }

    evt-0017 · correlationId calc-1 · causationId evt-0014

  42. 3.03 sProjection (read model)processedCustomerIdentityCreationRequestedcommand

  43. 2.7 sCustomer Identitypicked upCustomerIdentityCreationRequestedcommand

    attempt 1

  44. 2.68 sProjection (read model)picked upCustomerIdentityCreationRequestedcommand

    attempt 1

  45. 2.3 sProcess Manager (Saga)processedMortgageApprovedintegration

  46. 2.3 sProcess Manager (Saga)appended and publishedCustomerIdentityCreationRequestedcommand

    for: Customer IdentityProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000
    }

    evt-0014 · correlationId calc-1 · causationId evt-0011

  47. 2.23 sProjection (read model)processedMortgageApprovedintegration

  48. 1.98 sProjection (read model)picked upMortgageApprovedintegration

    attempt 1

  49. 1.95 sProcess Manager (Saga)picked upMortgageApprovedintegration

    attempt 1

  50. 1.65 sApproval / ControllingprocessedRiskAssessmentCompletedintegration

  51. 1.65 sApproval / Controllingappended and publishedMortgageApprovedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "approvedAmount": 5200000,
      "score": 800
    }

    evt-0011 · correlationId calc-1 · causationId evt-0008

  52. 1.5 sProjection (read model)processedRiskAssessmentCompletedintegration

  53. 1.13 sProjection (read model)picked upRiskAssessmentCompletedintegration

    attempt 1

  54. 1.1 sApproval / Controllingpicked upRiskAssessmentCompletedintegration

    attempt 1

  55. 0.8 sRisk ServiceprocessedMortgageApplicationSubmittedintegration

  56. 0.8 sRisk Serviceappended and publishedRiskAssessmentCompletedintegration

    for: Approval / ControllingProjection (read model)

    Event content
    {
      "score": 800,
      "loanAmount": 5200000,
      "monthlyIncome": 85000,
      "dti": 0.34811764705882353,
      "ltv": 0.8
    }

    evt-0008 · correlationId calc-1 · causationId evt-0004

  57. 0.75 sProcess Manager (Saga)processedMortgageApplicationSubmittedintegration

  58. 0.68 sProjection (read model)processedMortgageApplicationSubmittedintegration

  59. 0.4 sProcess Manager (Saga)picked upMortgageApplicationSubmittedintegration

    attempt 1

  60. 0.35 sProjection (read model)picked upMortgageApplicationSubmittedintegration

    attempt 1

  61. 0.28 sRisk Servicepicked upMortgageApplicationSubmittedintegration

    attempt 1

  62. 0.0 sMortgage Calculator APIappended and publishedMortgageApplicationSubmittedintegration

    for: Risk ServiceProcess Manager (Saga)Projection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "referenceNumber": "KALK-calc-1-2",
      "loanAmount": 5200000,
      "monthlyPayment": 29590,
      "monthlyIncome": 85000,
      "termYears": 30,
      "ratePct": 5.49,
      "dti": 0.34811764705882353,
      "ltv": 0.8
    }

    evt-0004 · correlationId calc-1 · causationId evt-0003

  63. 0.0 sMortgage Calculator APIappended to the event storeApplicationSubmitteddomain

    Event content
    {
      "calculationId": "calc-1",
      "referenceNumber": "KALK-calc-1-2",
      "inputs": {
        "monthlyIncome": 85000,
        "propertyPrice": 6500000,
        "downPaymentPct": 20,
        "ratePct": 5.49,
        "termYears": 30
      },
      "result": {
        "loan": 5200000,
        "monthlyPayment": 29590,
        "dti": 0.34811764705882353,
        "ltv": 0.8,
        "algorithm": "v1"
      }
    }

    evt-0003 · correlationId calc-1

  64. 0.0 sMortgage Calculator APIappended to the event storeCalculationCompleteddomain

    Event content
    {
      "algorithm": "v1",
      "result": {
        "loan": 5200000,
        "monthlyPayment": 29590,
        "dti": 0.34811764705882353,
        "ltv": 0.8,
        "algorithm": "v1"
      },
      "referenceNumber": "KALK-calc-1-2"
    }

    evt-0002 · correlationId calc-1

  65. 0.0 sMortgage Calculator APIappended to the event storeCalculationCreateddomain

    Event content
    {
      "calculationId": "calc-1",
      "monthlyIncome": 85000,
      "propertyPrice": 6500000,
      "downPaymentPct": 20,
      "ratePct": 5.49,
      "termYears": 30
    }

    evt-0001 · correlationId calc-1

Run with compensation

After MortgageDisbursementFailed the saga issues a command to close the account. The process ends in the compensated state and the system is consistent.

How the run unfolds (newest on top)

  1. 12.25 sProcess Manager (Saga)processedMortgageAccountClosedintegration

  2. 12.1 sProjection (read model)processedMortgageAccountClosedintegration

  3. 11.88 sProcess Manager (Saga)picked upMortgageAccountClosedintegration

    attempt 1

  4. 11.75 sProjection (read model)picked upMortgageAccountClosedintegration

    attempt 1

  5. 11.43 sMortgage AccountprocessedMortgageAccountClosureRequestedcommand

  6. 11.43 sMortgage Accountappended and publishedMortgageAccountClosedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "accountNumber": "0800-100017/0800"
    }

    evt-0044 · correlationId calc-1 · causationId evt-0041

  7. 11.15 sProjection (read model)processedMortgageAccountClosureRequestedcommand

  8. 10.83 sProjection (read model)picked upMortgageAccountClosureRequestedcommand

    attempt 1

  9. 10.75 sMortgage Accountpicked upMortgageAccountClosureRequestedcommand

    attempt 1

  10. 10.4 sProcess Manager (Saga)processedMortgageDisbursementFailedintegration

  11. 10.4 sProcess Manager (Saga)appended and publishedMortgageAccountClosureRequestedcommand

    for: Mortgage AccountProjection (read model)

    Event content
    {
      "accountNumber": "0800-100017/0800"
    }

    evt-0041 · correlationId calc-1 · causationId evt-0038

  12. 10.25 sProjection (read model)processedMortgageDisbursementFailedintegration

  13. 10.03 sProcess Manager (Saga)picked upMortgageDisbursementFailedintegration

    attempt 1

  14. 10.0 sProjection (read model)picked upMortgageDisbursementFailedintegration

    attempt 1

  15. 9.63 sDisbursementprocessedMortgageDisbursementRequestedcommand

  16. 9.63 sDisbursementappended and publishedMortgageDisbursementFailedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "reason": "Daily limit exceeded / AML check"
    }

    evt-0038 · correlationId calc-1 · causationId evt-0035

  17. 9.2 sProjection (read model)processedMortgageDisbursementRequestedcommand

  18. 8.95 sDisbursementpicked upMortgageDisbursementRequestedcommand

    attempt 1

  19. 8.88 sProjection (read model)picked upMortgageDisbursementRequestedcommand

    attempt 1

  20. 8.55 sProjection (read model)processedContractSignedintegration

  21. 8.5 sProcess Manager (Saga)processedContractSignedintegration

  22. 8.5 sProcess Manager (Saga)appended and publishedMortgageDisbursementRequestedcommand

    for: DisbursementProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "accountNumber": "0800-100017/0800",
      "customerId": "CUST-0001"
    }

    evt-0035 · correlationId calc-1 · causationId evt-0032

  23. 8.28 sProjection (read model)picked upContractSignedintegration

    attempt 1

  24. 8.23 sProcess Manager (Saga)picked upContractSignedintegration

    attempt 1

  25. 7.88 sDocument Serviceappended and publishedContractSignedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "contractId": "SML-0001",
      "signedBy": "klient"
    }

    evt-0032 · correlationId calc-1 · causationId evt-0029

  26. 7.03 sProjection (read model)processedContractGeneratedintegration

  27. 6.95 sProcess Manager (Saga)processedContractGeneratedintegration

  28. 6.68 sProjection (read model)picked upContractGeneratedintegration

    attempt 1

  29. 6.63 sProcess Manager (Saga)picked upContractGeneratedintegration

    attempt 1

  30. 6.35 sDocument ServiceprocessedContractGenerationRequestedcommand

  31. 6.35 sDocument Serviceappended and publishedContractGeneratedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "contractId": "SML-0001"
    }

    evt-0029 · correlationId calc-1 · causationId evt-0026

  32. 6.13 sProjection (read model)processedContractGenerationRequestedcommand

  33. 5.75 sProjection (read model)picked upContractGenerationRequestedcommand

    attempt 1

  34. 5.7 sDocument Servicepicked upContractGenerationRequestedcommand

    attempt 1

  35. 5.45 sProjection (read model)processedMortgageAccountOpenedintegration

  36. 5.33 sProcess Manager (Saga)processedMortgageAccountOpenedintegration

  37. 5.33 sProcess Manager (Saga)appended and publishedContractGenerationRequestedcommand

    for: Document ServiceProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "accountNumber": "0800-100017/0800",
      "customerId": "CUST-0001"
    }

    evt-0026 · correlationId calc-1 · causationId evt-0023

  38. 5.13 sProjection (read model)picked upMortgageAccountOpenedintegration

    attempt 1

  39. 4.98 sProcess Manager (Saga)picked upMortgageAccountOpenedintegration

    attempt 1

  40. 4.7 sMortgage AccountprocessedMortgageAccountOpeningRequestedcommand

  41. 4.7 sMortgage Accountappended and publishedMortgageAccountOpenedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "accountNumber": "0800-100017/0800"
    }

    evt-0023 · correlationId calc-1 · causationId evt-0020

  42. 4.45 sProjection (read model)processedMortgageAccountOpeningRequestedcommand

  43. 4.15 sMortgage Accountpicked upMortgageAccountOpeningRequestedcommand

    attempt 1

  44. 4.08 sProjection (read model)picked upMortgageAccountOpeningRequestedcommand

    attempt 1

  45. 3.88 sProjection (read model)processedCustomerIdentityCreatedintegration

  46. 3.75 sProcess Manager (Saga)processedCustomerIdentityCreatedintegration

  47. 3.75 sProcess Manager (Saga)appended and publishedMortgageAccountOpeningRequestedcommand

    for: Mortgage AccountProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000,
      "customerId": "CUST-0001"
    }

    evt-0020 · correlationId calc-1 · causationId evt-0017

  48. 3.58 sProjection (read model)picked upCustomerIdentityCreatedintegration

    attempt 1

  49. 3.45 sProcess Manager (Saga)picked upCustomerIdentityCreatedintegration

    attempt 1

  50. 3.18 sCustomer IdentityprocessedCustomerIdentityCreationRequestedcommand

  51. 3.18 sCustomer Identityappended and publishedCustomerIdentityCreatedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "customerId": "CUST-0001"
    }

    evt-0017 · correlationId calc-1 · causationId evt-0014

  52. 3.03 sProjection (read model)processedCustomerIdentityCreationRequestedcommand

  53. 2.7 sCustomer Identitypicked upCustomerIdentityCreationRequestedcommand

    attempt 1

  54. 2.68 sProjection (read model)picked upCustomerIdentityCreationRequestedcommand

    attempt 1

  55. 2.3 sProcess Manager (Saga)processedMortgageApprovedintegration

  56. 2.3 sProcess Manager (Saga)appended and publishedCustomerIdentityCreationRequestedcommand

    for: Customer IdentityProjection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "amount": 5200000
    }

    evt-0014 · correlationId calc-1 · causationId evt-0011

  57. 2.23 sProjection (read model)processedMortgageApprovedintegration

  58. 1.98 sProjection (read model)picked upMortgageApprovedintegration

    attempt 1

  59. 1.95 sProcess Manager (Saga)picked upMortgageApprovedintegration

    attempt 1

  60. 1.65 sApproval / ControllingprocessedRiskAssessmentCompletedintegration

  61. 1.65 sApproval / Controllingappended and publishedMortgageApprovedintegration

    for: Process Manager (Saga)Projection (read model)

    Event content
    {
      "approvedAmount": 5200000,
      "score": 800
    }

    evt-0011 · correlationId calc-1 · causationId evt-0008

  62. 1.5 sProjection (read model)processedRiskAssessmentCompletedintegration

  63. 1.13 sProjection (read model)picked upRiskAssessmentCompletedintegration

    attempt 1

  64. 1.1 sApproval / Controllingpicked upRiskAssessmentCompletedintegration

    attempt 1

  65. 0.8 sRisk ServiceprocessedMortgageApplicationSubmittedintegration

  66. 0.8 sRisk Serviceappended and publishedRiskAssessmentCompletedintegration

    for: Approval / ControllingProjection (read model)

    Event content
    {
      "score": 800,
      "loanAmount": 5200000,
      "monthlyIncome": 85000,
      "dti": 0.34811764705882353,
      "ltv": 0.8
    }

    evt-0008 · correlationId calc-1 · causationId evt-0004

  67. 0.75 sProcess Manager (Saga)processedMortgageApplicationSubmittedintegration

  68. 0.68 sProjection (read model)processedMortgageApplicationSubmittedintegration

  69. 0.4 sProcess Manager (Saga)picked upMortgageApplicationSubmittedintegration

    attempt 1

  70. 0.35 sProjection (read model)picked upMortgageApplicationSubmittedintegration

    attempt 1

  71. 0.28 sRisk Servicepicked upMortgageApplicationSubmittedintegration

    attempt 1

  72. 0.0 sMortgage Calculator APIappended and publishedMortgageApplicationSubmittedintegration

    for: Risk ServiceProcess Manager (Saga)Projection (read model)

    Event content
    {
      "applicationId": "calc-1",
      "referenceNumber": "KALK-calc-1-2",
      "loanAmount": 5200000,
      "monthlyPayment": 29590,
      "monthlyIncome": 85000,
      "termYears": 30,
      "ratePct": 5.49,
      "dti": 0.34811764705882353,
      "ltv": 0.8
    }

    evt-0004 · correlationId calc-1 · causationId evt-0003

  73. 0.0 sMortgage Calculator APIappended to the event storeApplicationSubmitteddomain

    Event content
    {
      "calculationId": "calc-1",
      "referenceNumber": "KALK-calc-1-2",
      "inputs": {
        "monthlyIncome": 85000,
        "propertyPrice": 6500000,
        "downPaymentPct": 20,
        "ratePct": 5.49,
        "termYears": 30
      },
      "result": {
        "loan": 5200000,
        "monthlyPayment": 29590,
        "dti": 0.34811764705882353,
        "ltv": 0.8,
        "algorithm": "v1"
      }
    }

    evt-0003 · correlationId calc-1

  74. 0.0 sMortgage Calculator APIappended to the event storeCalculationCompleteddomain

    Event content
    {
      "algorithm": "v1",
      "result": {
        "loan": 5200000,
        "monthlyPayment": 29590,
        "dti": 0.34811764705882353,
        "ltv": 0.8,
        "algorithm": "v1"
      },
      "referenceNumber": "KALK-calc-1-2"
    }

    evt-0002 · correlationId calc-1

  75. 0.0 sMortgage Calculator APIappended to the event storeCalculationCreateddomain

    Event content
    {
      "calculationId": "calc-1",
      "monthlyIncome": 85000,
      "propertyPrice": 6500000,
      "downPaymentPct": 20,
      "ratePct": 5.49,
      "termYears": 30
    }

    evt-0001 · correlationId calc-1

What happens in the simulator

A summary of the same scenario you run in the lab. The individual steps are shown with the runs above.

15 events · 10.4 s of simulated time

Cause → effect

  • Payout failed and the Process Manager has no compensation → The account and contract remained open without money: inconsistent state, nobody will clean it up MortgageDisbursementFailed

17 events · 12.3 s of simulated time

Cause → effect

  • The saga compensated the payout failure → The account was closed, the system is back in a consistent state MortgageAccountClosed

Try it in your own project

Copy the prompt into your coding agent (Claude Code, Copilot, Codex…) and run it in your project's repository.

Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

Language
Variant
# Task for a coding agent: Saga / Process Manager

Stack: Java. Mode: guided.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (Java backend (typically Java 21, Spring Boot, JPA or JDBC). Use the build tool and style the project already has (Maven/Gradle, JUnit 5).).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Specific task

Technical specifics (Java): Spring State Machine/Temporal/Axon Saga, if available in the organisation; otherwise a state table + handler.

Proceed step by step. After point 1 (plan), write what you found and what you propose, and wait for confirmation:
1. Plan: a table of steps (trigger, completion, timeout × attempts, compensation) and transitions. Final states: completed, rejected, failed, `compensated`, manual intervention.
2. Process Manager: persisted state per application (table with a version), reacts to events, issues commands (retry step, compensate, create task) only after saving the state.
3. Every step has a timeout and a retry policy; the attempt timeout is longer than the time the worker retries on its own. A step fails only after the PM's last attempt; a message in the DLQ is not a process failure.
4. Compensation for reversible steps (close the account) by application id, and manual intervention for irreversible ones (contract, payout). Once the process is stopped, workers skip the application's step.
5. Test: every step fails → the process ends in a defined state; compensation fails → manual intervention.

Watch out for:
- A saga is not a distributed transaction: inconsistency between steps is visible, and a late result may arrive even after compensation.
- Compensation can fail too.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- JUnit 5 tests in the existing `src/test/java` structure pass with the project's build tool.
- In the summary, state: what you changed, what you assumed, what remains.
# Task for a coding agent: Saga / Process Manager

Stack: Java. Mode: challenge.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (Java backend (typically Java 21, Spring Boot, JPA or JDBC). Use the build tool and style the project already has (Maven/Gradle, JUnit 5).).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Brief (Challenge)

Introduce a Process Manager for mortgage onboarding and ensure that a failure of any step ends in a consistent state.

Technical specifics (Java): Spring State Machine/Temporal/Axon Saga, if available in the organisation; otherwise a state table + handler.

Choose your own approach. The pitfalls in this topic are not listed: find them (concurrency, redelivery, ordering, failure midway) and for each write a test that would expose it. At the end, list which pitfalls you found and which you deliberately left open.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- JUnit 5 tests in the existing `src/test/java` structure pass with the project's build tool.
- In the summary, state: what you changed, what you assumed, what remains.
# Task for a coding agent: Saga / Process Manager

Stack: C#. Mode: guided.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (C# backend (.NET, ASP.NET Core, EF Core or Dapper). Use the solution, style and DI conventions the project already has (xUnit/NUnit).).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Specific task

Technical specifics (C#): MassTransit Saga/NServiceBus/Durable Functions, if available in the organisation; otherwise a state table + handler.

Proceed step by step. After point 1 (plan), write what you found and what you propose, and wait for confirmation:
1. Plan: a table of steps (trigger, completion, timeout × attempts, compensation) and transitions. Final states: completed, rejected, failed, `compensated`, manual intervention.
2. Process Manager: persisted state per application (table with a version), reacts to events, issues commands (retry step, compensate, create task) only after saving the state.
3. Every step has a timeout and a retry policy; the attempt timeout is longer than the time the worker retries on its own. A step fails only after the PM's last attempt; a message in the DLQ is not a process failure.
4. Compensation for reversible steps (close the account) by application id, and manual intervention for irreversible ones (contract, payout). Once the process is stopped, workers skip the application's step.
5. Test: every step fails → the process ends in a defined state; compensation fails → manual intervention.

Watch out for:
- A saga is not a distributed transaction: inconsistency between steps is visible, and a late result may arrive even after compensation.
- Compensation can fail too.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- Tests in the existing test project pass (`dotnet test`).
- In the summary, state: what you changed, what you assumed, what remains.
# Task for a coding agent: Saga / Process Manager

Stack: C#. Mode: challenge.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (C# backend (.NET, ASP.NET Core, EF Core or Dapper). Use the solution, style and DI conventions the project already has (xUnit/NUnit).).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Brief (Challenge)

Introduce a Process Manager for mortgage onboarding and ensure that a failure of any step ends in a consistent state.

Technical specifics (C#): MassTransit Saga/NServiceBus/Durable Functions, if available in the organisation; otherwise a state table + handler.

Choose your own approach. The pitfalls in this topic are not listed: find them (concurrency, redelivery, ordering, failure midway) and for each write a test that would expose it. At the end, list which pitfalls you found and which you deliberately left open.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- Tests in the existing test project pass (`dotnet test`).
- In the summary, state: what you changed, what you assumed, what remains.
# Task for a coding agent: Saga / Process Manager

Stack: Web (HTML/JS or the app's stack). Mode: guided.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (web application: a browser frontend, possibly with its own backend. If the project has a backend, the event store, broker and services belong on the backend, and the points about endpoints and tables refer to it. If it has no backend, use IndexedDB in the browser and `fake-indexeddb` in tests. Keep the project's framework and test runner; add new dependencies only with a justification.).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Specific task

Technical specifics (Web (HTML/JS or the app's stack)): Orchestration and durable state on the server; the UI only reads the state and displays it as a state machine (xstate, if the project may add a dependency; otherwise a pure function).

Proceed step by step. After point 1 (plan), write what you found and what you propose, and wait for confirmation:
1. Plan: a table of steps (trigger, completion, timeout × attempts, compensation) and transitions. Final states: completed, rejected, failed, `compensated`, manual intervention.
2. Process Manager: persisted state per application (table with a version), reacts to events, issues commands (retry step, compensate, create task) only after saving the state.
3. Every step has a timeout and a retry policy; the attempt timeout is longer than the time the worker retries on its own. A step fails only after the PM's last attempt; a message in the DLQ is not a process failure.
4. Compensation for reversible steps (close the account) by application id, and manual intervention for irreversible ones (contract, payout). Once the process is stopped, workers skip the application's step.
5. Test: every step fails → the process ends in a defined state; compensation fails → manual intervention.

Watch out for:
- A saga is not a distributed transaction: inconsistency between steps is visible, and a late result may arrive even after compensation.
- Compensation can fail too.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- Tests pass with the existing test runner; lint and build pass if the project has them.
- In the summary, state: what you changed, what you assumed, what remains.
# Task for a coding agent: Saga / Process Manager

Stack: Web (HTML/JS or the app's stack). Mode: challenge.
Goal: Introduce a Process Manager for mortgage onboarding with state, timeouts and compensation.

## Context of the existing code

You are working in an existing project (web application: a browser frontend, possibly with its own backend. If the project has a backend, the event store, broker and services belong on the backend, and the points about endpoints and tables refer to it. If it has no backend, use IndexedDB in the browser and `fake-indexeddb` in tests. Keep the project's framework and test runner; add new dependencies only with a justification.).
Domain: a mortgage calculator that is gradually turning into an event-driven system (calculator, risk, approval, identity, account, documents, payout).
Prerequisite: the project has asynchronous onboarding via a broker (identity → account → contract → payout). If not, first create a minimal in-memory flow of these steps.

Read first, change nothing. Find and briefly summarise:
- Where onboarding is currently coordinated by one large service/handler, or whether it is choreography. Do not change a synchronous handler with a public API.
- Which steps are reversible and by what (compensating action). Propose missing ones as an idempotent endpoint.
- Storage for the process state.
- Which of these parts already exist in the project and which are missing. Before writing your own infrastructure (process manager, saga), check whether the project or organisation already has it as a ready-made library, and use it. If it is missing, propose it in the plan.

## Expected behaviour

Onboarding spans several services and cannot be wrapped in a single transaction. The Process Manager (Saga) holds state per application, issues commands and reacts to results; a step that does not finish within its deadline is retried. When a step fails permanently, rollback is impossible: the PM reverts the completed reversible steps and creates a task for a human for the irreversible ones. Example: the payout fails (limit, AML); the PM issues a command to close the account (result `MortgageAccountClosed`), creates a task for the issued contract, and once it is resolved the process ends as `compensated`.

## Brief (Challenge)

Introduce a Process Manager for mortgage onboarding and ensure that a failure of any step ends in a consistent state.

Technical specifics (Web (HTML/JS or the app's stack)): Orchestration and durable state on the server; the UI only reads the state and displays it as a state machine (xstate, if the project may add a dependency; otherwise a pure function).

Choose your own approach. The pitfalls in this topic are not listed: find them (concurrency, redelivery, ordering, failure midway) and for each write a test that would expose it. At the end, list which pitfalls you found and which you deliberately left open.

## What you must not change

- The existing public API and the behaviour of the calculator endpoints that currently work.
- The database schema of existing tables (new tables/migrations are fine; renaming and deleting are not).
- Other people's tests, seed data and existing values. If a change is necessary, put it in a separate commit and state it. If a change to an event's shape or a new consumer necessarily changes an existing test, update it in a separate commit and explain why in the summary.
- Public contracts (APIs, event schemas) without backward compatibility.

## Done when

- A transition table and a test for the failure of each step.
- Process state survives a restart (test: a new instance over the same store continues).
- Compensations are idempotent; once the process is stopped, further steps are not executed.
- Tests pass with the existing test runner; lint and build pass if the project has them.
- In the summary, state: what you changed, what you assumed, what remains.

Training for your team

Would you like this training for your team?

We run the training on your code and your domain problem. We agree the scope, dates and format with you.

Discuss the training