|
| 1 | +# Saga Distributed Transactions Pattern on Azure |
| 2 | + |
| 3 | +This repository provides an implementation of the **Saga Distributed Transactions Pattern** using Azure services, including Azure Functions, Azure Service Bus, and Azure Cosmos DB. The Saga Pattern is used to manage distributed transactions across multiple microservices, ensuring data consistency and reliability even when some services fail. |
| 4 | + |
| 5 | +## 🏗️ Architectural Overview |
| 6 | + |
| 7 | +The **Saga Pattern** breaks a distributed transaction into a series of smaller transactions that are executed in a sequence. If any transaction fails, the Saga Pattern triggers compensating transactions to undo the changes made by previous steps, ensuring **eventual consistency** across the entire system. |
| 8 | + |
| 9 | +### Architecture Diagram |
| 10 | + |
| 11 | +```mermaid |
| 12 | +graph TD |
| 13 | + Client["Client"] -->|Initiates Transaction| OrchestratorFunction["Orchestrator Function (Azure Function)"] |
| 14 | + OrchestratorFunction -->|Manages Steps| ActivityFunction["Activity Function (Azure Function)"] |
| 15 | + OrchestratorFunction -->|Compensation Triggered| CompensatorFunction["Compensator Function (Azure Function)"] |
| 16 | + ActivityFunction -->|Calls| ServiceBus["Azure Service Bus"] |
| 17 | + ServiceBus -->|Triggers| CompensatorFunction |
| 18 | + ActivityFunction -->|Manipulates| CosmosDB["Azure Cosmos DB"] |
| 19 | + CompensatorFunction -->|Ensures| CosmosDB |
| 20 | + OrchestratorFunction -->|Monitors| AppInsights["Application Insights"] |
| 21 | + ActivityFunction -->|Monitors| AppInsights |
| 22 | + CompensatorFunction -->|Monitors| AppInsights |
| 23 | +``` |
| 24 | + |
| 25 | +### Components of the Architecture |
| 26 | + |
| 27 | +1. **Client**: Initiates a transaction request to the **Orchestrator Function**. |
| 28 | +2. **Orchestrator Function**: Manages the overall workflow of the Saga, coordinating the execution of the **Activity Function** and triggering the **Compensator Function** in case of a failure. |
| 29 | +3. **Activity Function**: Performs individual steps of the transaction, such as processing business logic and writing to **Azure Cosmos DB**. |
| 30 | +4. **Compensator Function**: Reverts or compensates for changes if a failure occurs in any step of the Saga. |
| 31 | +5. **Azure Service Bus**: Acts as a messaging backbone for communication between functions, triggering compensating transactions when necessary. |
| 32 | +6. **Azure Cosmos DB**: Serves as the data store where the transaction data is stored. |
| 33 | +7. **Application Insights**: Collects logs, metrics, and telemetry data for monitoring and troubleshooting purposes. |
| 34 | + |
| 35 | +## 📂 Repository Structure |
| 36 | + |
| 37 | +``` |
| 38 | +/saga-distributed-transactions |
| 39 | +│ |
| 40 | +├── README.md # Root README with architecture overview and getting started |
| 41 | +├── LICENSE # MIT License |
| 42 | +│ |
| 43 | +├── infrastructure |
| 44 | +│ ├── README.md # README for Infrastructure deployment |
| 45 | +│ ├── azure-resources.bicep # Bicep template for all Azure resources |
| 46 | +│ └── .github/workflows/deploy-bicep.yml # GitHub Action to deploy Azure resources |
| 47 | +│ |
| 48 | +├── orchestrator-function |
| 49 | +│ ├── README.md # README for Orchestrator Function |
| 50 | +│ ├── orchestrator_function.py # Python code for Orchestrator Function |
| 51 | +│ ├── requirements.txt # Dependencies for Orchestrator Function |
| 52 | +│ └── .github/workflows/deploy-orchestrator.yml # GitHub Action to deploy the Orchestrator Function |
| 53 | +│ |
| 54 | +├── activity-function |
| 55 | +│ ├── README.md # README for Activity Function |
| 56 | +│ ├── activity_function.py # Python code for Activity Function |
| 57 | +│ ├── requirements.txt # Dependencies for Activity Function |
| 58 | +│ └── .github/workflows/deploy-activity.yml # GitHub Action to deploy the Activity Function |
| 59 | +│ |
| 60 | +├── compensator-function |
| 61 | +│ ├── README.md # README for Compensator Function |
| 62 | +│ ├── compensator_function.py # Python code for Compensator Function |
| 63 | +│ ├── requirements.txt # Dependencies for Compensator Function |
| 64 | +│ └── .github/workflows/deploy-compensator.yml # GitHub Action to deploy the Compensator Function |
| 65 | +``` |
| 66 | + |
| 67 | +## 🚀 Getting Started |
| 68 | + |
| 69 | +### Step 1: Deploy the Infrastructure |
| 70 | + |
| 71 | +1. Navigate to the **`infrastructure`** folder. |
| 72 | +2. Follow the instructions in the [Infrastructure README](infrastructure/README.md) to deploy the required Azure resources using the Bicep template and GitHub Actions. |
| 73 | + |
| 74 | +### Step 2: Deploy the Azure Functions |
| 75 | + |
| 76 | +1. Deploy the **Orchestrator Function**: |
| 77 | + - Navigate to the **`orchestrator-function`** folder. |
| 78 | + - Follow the instructions in the [Orchestrator Function README](orchestrator-function/README.md) to deploy the function using GitHub Actions. |
| 79 | + |
| 80 | +2. Deploy the **Activity Function**: |
| 81 | + - Navigate to the **`activity-function`** folder. |
| 82 | + - Follow the instructions in the [Activity Function README](activity-function/README.md) to deploy the function using GitHub Actions. |
| 83 | + |
| 84 | +3. Deploy the **Compensator Function**: |
| 85 | + - Navigate to the **`compensator-function`** folder. |
| 86 | + - Follow the instructions in the [Compensator Function README](compensator-function/README.md) to deploy the function using GitHub Actions. |
| 87 | + |
| 88 | +### Step 3: Running the Solution |
| 89 | + |
| 90 | +1. **Trigger the Orchestrator Function**: |
| 91 | + - Use an HTTP client (like Postman or `curl`) to send a POST request to the `/startSaga` endpoint of the Orchestrator Function. |
| 92 | + |
| 93 | +2. **Monitor the Workflow**: |
| 94 | + - Check **Application Insights** in the Azure portal for logs and telemetry data to monitor the execution flow and identify any issues. |
| 95 | + |
| 96 | +## 💡 How It Works |
| 97 | + |
| 98 | +1. **Initiate a Transaction**: |
| 99 | + - The **Client** initiates a transaction request to the **Orchestrator Function** by sending an HTTP request with order details. |
| 100 | + |
| 101 | +2. **Orchestrator Function Manages Workflow**: |
| 102 | + - The **Orchestrator Function** processes the request, calls the **Activity Function** to handle the business logic, and waits for a success or failure response. |
| 103 | + |
| 104 | +3. **Activity Function Processes Data**: |
| 105 | + - The **Activity Function** receives the order data and writes it to **Azure Cosmos DB**. |
| 106 | + - If the transaction is successful, it completes the process; if there is an error, the **Compensator Function** is triggered. |
| 107 | + |
| 108 | +4. **Compensator Function Handles Failures**: |
| 109 | + - The **Compensator Function** is triggered if a failure occurs during the transaction. |
| 110 | + - It compensates for the failure by removing or rolling back any changes made to **Azure Cosmos DB**. |
| 111 | + |
| 112 | +5. **Application Insights Monitors the Flow**: |
| 113 | + - All functions log their activities to **Application Insights** for monitoring and troubleshooting purposes. |
| 114 | + |
| 115 | +## 📝 About the Saga Pattern |
| 116 | + |
| 117 | +The **Saga Pattern** is a design pattern for managing distributed transactions in microservices architecture. It breaks down a large transaction into smaller sub-transactions that are executed in a sequence, ensuring eventual consistency across the system. |
| 118 | + |
| 119 | +- **Resilience**: By breaking down transactions into smaller steps and defining compensating actions, the Saga Pattern helps systems recover from failures gracefully. |
| 120 | +- **Scalability**: Decoupling transactions across services improves scalability, allowing independent scaling of microservices. |
| 121 | +- **Flexibility**: The Saga Pattern supports various consistency models, including eventual consistency, which is crucial for distributed systems. |
| 122 | + |
| 123 | +## 📄 License |
| 124 | + |
| 125 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 126 | + |
| 127 | +## 🙌 Contributing |
| 128 | + |
| 129 | +Contributions are welcome! Please open an issue or submit a pull request for any improvements or suggestions. |
0 commit comments