|
1 | 1 | --- |
2 | 2 | title: Commander |
3 | | -category: Concurrency |
| 3 | +category: Behavioral |
4 | 4 | language: en |
5 | 5 | tag: |
6 | | - - Cloud distributed |
| 6 | + - Cloud distributed |
| 7 | + - Microservices |
| 8 | + - Transactions |
7 | 9 | --- |
8 | 10 |
|
| 11 | +## Also known as |
| 12 | + |
| 13 | +* Distributed Transaction Commander |
| 14 | +* Transaction Coordinator |
| 15 | + |
9 | 16 | ## Intent |
10 | 17 |
|
11 | | -> Used to handle all problems that can be encountered when doing distributed transactions. |
| 18 | +The intent of the Commander pattern in the context of distributed transactions is to manage and coordinate complex |
| 19 | +transactions across multiple distributed components or services, ensuring consistency and integrity of the overall |
| 20 | +transaction. It encapsulates transaction commands and coordination logic, facilitating the implementation of distributed |
| 21 | +transaction protocols like two-phase commit or Saga. |
| 22 | + |
| 23 | +## Explanation |
| 24 | + |
| 25 | +Real-world example |
| 26 | + |
| 27 | +> Imagine organizing a large international music festival where various bands from around the world are scheduled to |
| 28 | +> perform. Each band's arrival, soundcheck, and performance are like individual transactions in a distributed system. The |
| 29 | +> festival organizer acts as the "Commander," coordinating these transactions to ensure that if a band's flight is |
| 30 | +> delayed (akin to a transaction failure), there's a backup plan, such as rescheduling or swapping time slots with another |
| 31 | +> band (compensating actions), to keep the overall schedule intact. This setup mirrors the Commander pattern in |
| 32 | +> distributed transactions, where various components must be coordinated to achieve a successful outcome despite |
| 33 | +> individual failures. |
| 34 | +
|
| 35 | +In plain words |
| 36 | + |
| 37 | +> The Commander pattern turns a request into a stand-alone object, allowing for the parameterization of commands, |
| 38 | +> queueing of actions, and the implementation of undo operations. |
| 39 | +
|
| 40 | +**Programmatic Example** |
| 41 | + |
| 42 | +Managing transactions across different services in a distributed system, such as an e-commerce platform with separate |
| 43 | +Payment and Shipping microservices, requires careful coordination to avoid issues. When a user places an order but one |
| 44 | +service (e.g., Payment) is unavailable while the other (e.g., Shipping) is ready, we need a robust solution to handle |
| 45 | +this discrepancy. |
| 46 | + |
| 47 | +A strategy to address this involves using a Commander component that orchestrates the process. Initially, the order is |
| 48 | +processed by the available service (Shipping in this case). The commander then attempts to synchronize the order with |
| 49 | +the currently unavailable service (Payment) by storing the order details in a database or queueing it for future |
| 50 | +processing. This queueing system must also account for possible failures in adding requests to the queue. |
| 51 | + |
| 52 | +The commander repeatedly tries to process the queued orders to ensure both services eventually reflect the same |
| 53 | +transaction data. This process involves ensuring idempotence, meaning that even if the same order synchronization |
| 54 | +request is made multiple times, it will only be executed once, preventing duplicate transactions. The goal is to achieve |
| 55 | +eventual consistency across services, where all systems are synchronized over time despite initial failures or delays. |
| 56 | + |
| 57 | +In the provided code, the Commander pattern is used to handle distributed transactions across multiple services ( |
| 58 | +PaymentService, ShippingService, MessagingService, EmployeeHandle). Each service has its own database and can throw |
| 59 | +exceptions to simulate failures. |
| 60 | + |
| 61 | +The Commander class is the central part of this pattern. It takes instances of all services and their databases, along |
| 62 | +with some configuration parameters. The placeOrder method in the Commander class is used to place an order, which |
| 63 | +involves interacting with all the services. |
| 64 | + |
| 65 | +```java |
| 66 | +public class Commander { |
| 67 | + // ... constructor and other methods ... |
| 68 | + |
| 69 | + public void placeOrder(Order order) { |
| 70 | + // ... implementation ... |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The User and Order classes represent a user and an order respectively. An order is placed by a user. |
| 76 | + |
| 77 | +```java |
| 78 | +public class User { |
| 79 | + // ... constructor and other methods ... |
| 80 | +} |
| 81 | + |
| 82 | +public class Order { |
| 83 | + // ... constructor and other methods ... |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Each service (e.g., PaymentService, ShippingService, MessagingService, EmployeeHandle) has its own database and can |
| 88 | +throw exceptions to simulate failures. For example, the PaymentService might throw a DatabaseUnavailableException if its |
| 89 | +database is unavailable. |
| 90 | + |
| 91 | +```java |
| 92 | +public class PaymentService { |
| 93 | + // ... constructor and other methods ... |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +The DatabaseUnavailableException, ItemUnavailableException, and ShippingNotPossibleException classes represent different |
| 98 | +types of exceptions that can occur. |
| 99 | + |
| 100 | +```java |
| 101 | +public class DatabaseUnavailableException extends Exception { |
| 102 | + // ... constructor and other methods ... |
| 103 | +} |
| 104 | + |
| 105 | +public class ItemUnavailableException extends Exception { |
| 106 | + // ... constructor and other methods ... |
| 107 | +} |
| 108 | + |
| 109 | +public class ShippingNotPossibleException extends Exception { |
| 110 | + // ... constructor and other methods ... |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +In the main method of each class (AppQueueFailCases, AppShippingFailCases), different scenarios are simulated by |
| 115 | +creating instances of the Commander class with different configurations and calling the placeOrder method. |
12 | 116 |
|
13 | 117 | ## Class diagram |
| 118 | + |
14 | 119 |  |
15 | 120 |
|
16 | 121 | ## Applicability |
17 | | -This pattern can be used when we need to make commits into 2 (or more) databases to complete transaction, which cannot be done atomically and can thereby create problems. |
18 | 122 |
|
19 | | -## Explanation |
20 | | -Handling distributed transactions can be tricky, but if we choose to not handle it carefully, there could be unwanted consequences. Say, we have an e-commerce website which has a Payment microservice and a Shipping microservice. If the shipping is available currently but payment service is not up, or vice versa, how would we deal with it after having already received the order from the user? |
21 | | -We need a mechanism in place which can handle these kinds of situations. We have to direct the order to either one of the services (in this example, shipping) and then add the order into the database of the other service (in this example, payment), since two databases cannot be updated atomically. If currently unable to do it, there should be a queue where this request can be queued, and there has to be a mechanism which allows for a failure in the queueing as well. All this needs to be done by constant retries while ensuring idempotence (even if the request is made several times, the change should only be applied once) by a commander class, to reach a state of eventual consistency. |
| 123 | +Use the Commander pattern for distributed transactions when: |
| 124 | + |
| 125 | +* You need to ensure data consistency across distributed services in the event of partial system failures. |
| 126 | +* Transactions span multiple microservices or distributed components requiring coordinated commit or rollback. |
| 127 | +* You are implementing long-lived transactions requiring compensating actions for rollback. |
| 128 | + |
| 129 | +## Known Uses |
| 130 | + |
| 131 | +* Two-Phase Commit (2PC) Protocols: Coordinating commit or rollback across distributed databases or services. |
| 132 | +* Saga Pattern Implementations: Managing long-lived business processes that span multiple microservices, with each step |
| 133 | + having a compensating action for rollback. |
| 134 | +* Distributed Transactions in Microservices Architecture: Coordinating complex operations across microservices while |
| 135 | + maintaining data integrity and consistency. |
| 136 | + |
| 137 | +## Consequences |
| 138 | + |
| 139 | +Benefits: |
| 140 | + |
| 141 | +* Provides a clear mechanism for managing complex distributed transactions, enhancing system reliability. |
| 142 | +* Enables the implementation of compensating transactions, which are crucial for maintaining consistency in long-lived |
| 143 | + transactions. |
| 144 | +* Facilitates the integration of heterogeneous systems within a transactional context. |
| 145 | + |
| 146 | +Trade-offs: |
| 147 | + |
| 148 | +* Increases complexity, especially in failure scenarios, due to the need for coordinated rollback mechanisms. |
| 149 | +* Potentially impacts performance due to the overhead of coordination and consistency checks. |
| 150 | +* Saga-based implementations can lead to increased complexity in understanding the overall business process flow. |
| 151 | + |
| 152 | +## Related Patterns |
| 153 | + |
| 154 | +[Saga Pattern](https://java-design-patterns.com/patterns/saga/): Often discussed in tandem with the Commander pattern |
| 155 | +for distributed transactions, focusing on long-lived transactions with compensating actions. |
22 | 156 |
|
23 | 157 | ## Credits |
24 | 158 |
|
25 | 159 | * [Distributed Transactions: The Icebergs of Microservices](https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/) |
| 160 | +* [Microservices Patterns: With examples in Java](https://amzn.to/4axjnYW) |
| 161 | +* [Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems](https://amzn.to/4axHwOV) |
| 162 | +* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/4aATcRe) |
0 commit comments