POSA Design Patterns
What is POSA?
POSA, which stands for Pattern-Oriented Software Architecture, is a series of books that catalogue and describe fundamental patterns for building robust and resilient software systems. Unlike the more focused Gang of Four (GoF) patterns for object-oriented design, POSA patterns address broader architectural and design issues, particularly in areas like concurrent, distributed, and networked systems. The series provides a common language and a systematic approach for tackling complex software design challenges.
This section focuses on the design patterns from the POSA series that solve intermediate-level design problems, often related to object collaboration or managing concurrency and network communication.
The POSA series is divided into two main volumes: POSA1, which focuses on fundamental design patterns, and POSA2, which covers patterns for concurrent and networked objects.
graph TD A[PoSA Patterns] --> A1(POSA1 Design Patterns); A[PoSA Patterns] --> A2(POSA2 Patterns for Concurrent and Networked Objects); A1 --> A1_F(POSA1 Fundamental Patterns); A2 --> A2_B(Event Handling & Concurrency Primitives); A2 --> A2_C(Resilience and Fault Tolerance); A2 --> A2_E(Deployment and Infrastructure);
POSA1 Design Patterns
This section focuses on the design patterns described in POSA Volume 1: A System of Patterns. These patterns often serve as a bridge between high-level architectural decisions and object-oriented design.
POSA1 Fundamental Patterns
graph TD A1_F(POSA1 Fundamental Patterns); A1_F --> P1(Whole-Part); A1_F --> P2(Master-Slave); A1_F --> P3(Proxy); A1_F --> P4(Command Processor); A1_F --> P5(View Handler); A1_F --> P6(Forwarder-Receiver); A1_F --> P7(Client-Dispatcher-Server); A1_F --> P8(Publisher-Subscriber);
Whole-Part
-
Problem: How to organize objects that are the components of a larger, composite object?
-
Synopsis: The Whole-Part (or Composition) pattern allows for structuring a composite aggregate of objects. The Whole is responsible for managing the collection of its Parts and coordinating their interactions. This pattern is foundational for any system that deals with hierarchical or composite data, like a company’s organizational chart or a document’s structure.
classDiagram class Whole { + List~Part~ parts + operationA() + operationB() - manageParts() } class Part { - data + behavior() } Whole "1" *-- "many" Part Client --> Whole : usesThis class diagram illustrates the compositional structure of the
Whole-Partpattern. It shows aWholeclass that maintains a collection ofPartobjects. It also depicts theWholeas responsible for the lifecycle of itsPartsand shows how a client interacts with theWholeto perform operations on the collection. -
Key Characteristics:
- Composition: The parts are fully contained within the whole. If the
Wholeis destroyed, itsPartsare typically destroyed as well. This creates a strong, “has-a” relationship. - Unified interface: Clients interact with the
Wholevia a single interface, abstracting away the complexity of its internal structure. - Encapsulation: The
Wholeencapsulates the behavior and data of itsParts, providing a single point of control.
- Composition: The parts are fully contained within the whole. If the
-
Applicability: This pattern is ideal for any system where objects are logically composed of other objects, such as a graphical user interface composed of widgets, or a complex order composed of individual line items.
-
Limitations and Challenges: Can lead to a monolithic
Wholeobject with too many responsibilities. The management of complex interactions betweenPartscan be difficult to manage. -
Relationship with Other Patterns:
- The Whole-Part pattern is fundamentally the same as the Composite pattern from the GoF book, which allows clients to treat individual objects and compositions of objects uniformly.
- It can be used within an MVC architecture, where a complex
View(theWhole) is composed of smaller UI widgets (theParts). - The
Partscan be lazily loaded using the Proxy pattern to improve performance.
Master-Slave
-
Problem: How to decompose a complex and resource-intensive task into smaller sub-tasks that can be executed by independent entities?
-
Synopsis: The Master-Slave pattern separates the coordination logic of a task from its execution. The
Masteris responsible for decomposing the task, distributing sub-tasks to theSlaves, and aggregating the final results. This pattern is a fundamental approach for parallel and distributed computing. Note: In modern terminology, this pattern is often referred to as Leader-Follower, Primary-Secondary, or Controller-Worker to use more inclusive language.sequenceDiagram participant Client participant Master participant Slave1 participant Slave2 participant Slave3 Client->>Master: Request for Complex Task activate Master Master-->>Master: Decompose into Sub-tasks Master->>Slave1: Dispatch Sub-task A Master->>Slave2: Dispatch Sub-task B Master->>Slave3: Dispatch Sub-task C Slave1-->>Master: Result A Slave2-->>Master: Result B Slave3-->>Master: Result C Master-->>Master: Aggregate Results Master-->>Client: Final Result deactivate MasterThis sequence diagram illustrates the delegation of work from the
Masterto itsSlaves. It shows theMasterfirst decomposing a complex task, then sending individual sub-tasks to eachSlave. Finally, it depicts theMasterreceiving and aggregating the results from eachSlaveto form the final outcome. -
Key Characteristics:
- Task decomposition: The
Masterdivides the problem into manageable, independent sub-problems. - Delegation: The
Slavesperform the computations, often in parallel, without needing to communicate with each other. - Results aggregation: The
Masterreassembles the individual results from theSlavesinto a single, cohesive final solution.
- Task decomposition: The
-
Applicability: This pattern is commonly used in parallel computing, data processing systems (e.g., MapReduce frameworks), and large-scale search algorithms.
-
Limitations and Challenges: Performance depends heavily on the ability to effectively decompose the task. Synchronization and managing failures of the
Slaves(e.g., what if aSlavecrashes?) can be complex. -
Relationship with Other Patterns:
- The Master-Slave pattern is a specific implementation of task parallelism and is related to patterns like Fork/Join.
- The
Mastercan use the Command Processor pattern to create and manage the sub-tasks that are sent to theSlaves. - In distributed systems, this pattern is often a building block for more complex architectures like MapReduce.
Proxy
- Synopsis: The Proxy pattern provides a substitute or placeholder for another object to control access to it. It is one of the original GoF patterns and is used to add functionality like lazy loading, access control, or remote communication. For a detailed explanation, see the main GoF Design Patterns page.
- Architectural Context: In system architecture, this pattern is the foundation for the Architectural Proxy Pattern, which includes network-level intermediaries like Forward Proxies and Reverse Proxies.
Command Processor
-
Problem: How to encapsulate requests so they can be handled transparently, queued, logged, or undone?
-
Synopsis: The Command Processor pattern separates a command’s invocation from its execution. A command is encapsulated in a
Commandobject that can be stored in a queue. AProcessorthen executes these commands sequentially, providing a robust mechanism for managing a sequence of actions.sequenceDiagram participant Client participant Command participant CommandProcessor participant Receiver Client->>Command: create() Client->>CommandProcessor: execute(command) CommandProcessor->>CommandProcessor: queue(command) CommandProcessor->>Command: execute() Command->>Receiver: action()This sequence diagram illustrates the flow of a command through the
Command Processorpattern. It shows aClientcreating and invoking aCommandobject on aCommand Processor. TheProcessorthen manages a queue of commands and executes them in sequence, decoupling the invocation from the execution. -
Key Characteristics:
- Command encapsulation: The request is a first-class object, allowing it to be passed around, stored, and executed later.
- Queueing: Commands are stored in a queue, enabling features like batch processing and scheduling.
- Extended functionality: The pattern’s structure makes it easy to add features like a transaction log (for persistence), undo/redo functionality, and macros.
-
Applicability: This pattern is a cornerstone of text editors (
undo/redofunctionality), workflow systems, and any application that needs to manage a history of operations. -
Limitations and Challenges: Can introduce complexity for simple applications. Implementing
undo/redocan be difficult for commands that have side effects. -
Relationship with Other Patterns:
- This pattern is an extension of the Command pattern from the GoF book. It adds the “processor” component that manages a queue of commands.
- It can be used in a Master-Slave architecture, where the
MastercreatesCommandobjects and sends them toSlavesfor execution. - The queueing mechanism is a form of Message Queue.
View Handler
-
Problem: How to manage the dependencies and coordination between multiple views in an interactive system?
-
Synopsis: The View Handler pattern manages the creation, destruction, and switching between different views of an application. It acts as a coordinator that ensures the views’ consistency with the data model. This pattern is crucial for maintaining a clean separation between the user interface and the underlying business logic.
graph TD subgraph System subgraph Views View1 View2 View3 end ViewHandler Model end ViewHandler --> View1 ViewHandler --> View2 ViewHandler --> View3 ViewHandler <--> ModelThis component diagram shows the responsibilities of the
View Handler. It illustrates theView Handlercomponent managing multipleViewcomponents. It also shows theView Handler’s role in communicating with the underlyingModelto ensure that allViewsare synchronized with the state of the application data. -
Key Characteristics:
- Centralized coordination: The
View Handlerserves as a single point of control for managing all views, simplifying the overall architecture. - State management: It is responsible for handling view state changes and ensuring that views are properly synchronized with the
Model. - Decoupling: It decouples the
Viewsfrom each other, as they only need to communicate with theView Handleror theModeldirectly.
- Centralized coordination: The
-
Applicability: This pattern is fundamental to GUI frameworks,
single-pageweb applications (SPAs), and dashboard systems where multiple views of the same data need to be managed. -
Limitations and Challenges: The
View Handlercan become a god object if it accumulates too much coordination logic. To mitigate this, complex views can be further decomposed using smaller, nested controllers or mediators that manage subsets of the UI, distributing the responsibility. -
Relationship with Other Patterns:
- The View Handler is a key component in presentation patterns like MVC, MVP, and MVVM, where it often plays the role of the
ControllerorPresenter. - It frequently manages a Whole-Part structure, where the main view is composed of smaller, nested views.
- It often works with the Publisher-Subscriber pattern to receive updates from the
Modeland refresh theViews.
- The View Handler is a key component in presentation patterns like MVC, MVP, and MVVM, where it often plays the role of the
Forwarder-Receiver
-
Problem: How to provide transparent point-to-point communication in a distributed system, hiding network details?
-
Synopsis: The Forwarder-Receiver pattern separates communication layers from the applications that use them. A Forwarder is responsible for sending messages from one process to another, while the Receiver receives and distributes incoming messages. This pattern is often used as a building block for more complex communication systems.
graph TD Client --> Forwarder Forwarder --> Receiver Receiver --> Server subgraph Host1 Client Forwarder end subgraph Host2 Receiver Server endThis component diagram illustrates the
Forwarder-Receiverpattern. It shows aClientinteracting with aForwardercomponent to send a message. TheForwarderhandles the network details and passes the message to a remoteReceivercomponent, which then delivers the message to the intendedServer. This highlights the encapsulation of communication logic. -
Key Characteristics:
- Encapsulation of transport: Network protocols, serialization, and other low-level details are hidden from the application logic.
- Synchronous communication: It is often used for request-response interactions where the client waits for a reply.
- Simplicity: Provides a relatively straightforward way to achieve point-to-point communication without building a complex messaging system.
-
Applicability: This pattern is suitable for
Inter-Process Communication(IPC) and simple distributed systems where direct, encapsulated communication is required. -
Limitations and Challenges: It does not natively handle distributing messages to multiple recipients. It is also not suitable for systems that require a high degree of loose coupling or asynchronous communication.
-
Relationship with Other Patterns:
- The Forwarder-Receiver pattern is a foundational pattern for building distributed systems and can be seen as a simplified version of the Broker pattern.
- It is often used with the Proxy pattern, where the
Forwarderacts as a local proxy for a remote service, hiding the network communication details from the client.
Client-Dispatcher-Server
-
Problem: How to manage communication between clients and servers in a flexible way, hiding the location of services?
-
Synopsis: The Client-Dispatcher-Server pattern introduces a central
Dispatchercomponent that acts as a broker.Clientssend their requests to theDispatcher, which then routes them to the appropriateServer. This pattern provides a level of indirection that simplifies service management and adds flexibility.graph TD Client --> Dispatcher Dispatcher --> Server1 Dispatcher --> Server2 Dispatcher --> Server3 subgraph "Servers" Server1 Server2 Server3 endThis component diagram shows the
Client-Dispatcher-Serverpattern. It illustrates how theClientsends a request to a centralDispatchercomponent. TheDispatcheris then responsible for locating the correctServerand forwarding the request. This centralized approach hides the service location from the client. -
Key Characteristics:
- Centralization: A single point of coordination (
Dispatcher) simplifies [[service-discovery|service discovery] and routing. - Flexibility: It allows for adding, removing, or moving
serverswithout affecting client code. - Decoupling:
Clientsdo not need to know the location of theServers, only the address of theDispatcher.
- Centralization: A single point of coordination (
-
Applicability: This pattern is well-suited for dynamic distributed systems, service registries, and microservices architectures where services may be added or removed frequently.
-
Limitations and Challenges: The
Dispatchercan become a performance bottleneck or asingle point of failure. A robust implementation requires built-in redundancy and failover mechanisms. -
Relationship with Other Patterns:
- This pattern is a specific implementation of the Broker pattern, where the
Dispatcheracts as the central message broker. - It is conceptually similar to the Service Locator pattern.
- In modern microservices architectures, the
Dispatcherrole is often fulfilled by an API Gateway or a combination of a Service Registry and [[service-discovery|Service Discovery] mechanism.
- This pattern is a specific implementation of the Broker pattern, where the
Publisher-Subscriber
-
Problem: How to enable asynchronous and decoupled communication between entities?
-
Synopsis: The Publisher-Subscriber pattern allows entities (Subscribers) to subscribe to events or messages of interest. A Publisher emits messages without knowing the
Subscribers, which creates a loose coupling between them. This pattern is the cornerstone of event-driven architectures.graph TD Publisher1 --> Broker Publisher2 --> Broker Broker --> Subscriber1 Broker --> Subscriber2This component diagram illustrates the
Publisher-Subscriberpattern. It showsPublisherssending messages to a centralChannelorBroker. TheSubscribersare registered with theChanneland receive messages without being aware of thePublishers. This highlights the asynchronous and decoupled nature of the communication. -
Key Characteristics:
- Loose coupling:
PublishersandSubscribersare not aware of each other, communicating through a shared channel. - Asynchronous: Communication is non-blocking, allowing
Publishersto continue processing without waiting forSubscribersto receive the message. - Scalability: The pattern is highly scalable, as new
PublishersorSubscriberscan be added without modifying existing components.
- Loose coupling:
-
Applicability: This pattern is fundamental to real-time data streams, notification systems, and microservices that communicate via events.
-
Limitations and Challenges: The reliability of message delivery can be a challenge. The system’s complexity increases if message ordering or guaranteed delivery is required.
-
Relationship with Other Patterns:
- The Publisher-Subscriber pattern is a more scalable and decoupled version of the Observer pattern from the GoF book.
- It is the foundation of event-driven architectures and is often implemented using a Message Queue or a Broker.
- In microservices, this pattern is the primary way services communicate asynchronously.
POSA2 Patterns for Concurrent and Networked Objects
graph TD A2(POSA2 Patterns for Concurrent and Networked Objects); %% POSA2 Patterns A2 --> A2_B(Event Handling & Concurrency Primitives); A2 --> A2_C(Resilience and Fault Tolerance); A2 --> A2_D(Coordination); A2 --> A2_E(Deployment and Infrastructure);
Event Handling & Concurrency Primitives
graph TD A2_B(Event Handling & Concurrency Primitives); A2_B --> B1(Reactor); A2_B --> B2(Proactor); A2_B --> B3(Acceptor-Connector); A2_B --> B4(Active Object);
Reactor
-
Problem: How to efficiently handle multiple service requests from various sources without blocking the program’s execution?
-
Synopsis: The
Reactorpattern demultiplexes events from multiple sources and dispatches them to their correspondinghandlers. It’s the foundation of many non-blocking I/O frameworks.sequenceDiagram participant Client1 participant Client2 participant Reactor participant EventHandler1 participant EventHandler2 Client1->>Reactor: Service Request 1 (e.g., connection, data) Client2->>Reactor: Service Request 2 (e.g., connection, data) Reactor->>Reactor: Wait for events on handles (non-blocking) Note over Reactor: Event detected on handle for Client1 Reactor->>EventHandler1: Dispatch Event 1 EventHandler1->>Client1: Process Request 1 & Send Response Note over Reactor: Event detected on handle for Client2 Reactor->>EventHandler2: Dispatch Event 2 EventHandler2->>Client2: Process Request 2 & Send ResponseThis sequence diagram illustrates the event-driven behavior of the
Reactorpattern. It shows how theReactorwaits for events from multiple clients. When an event is detected on a handle, theReactordispatches it to the appropriateEventHandlerto be processed, allowing the system to handle multiple requests concurrently without blocking. -
Key Characteristics:
- Event-driven model: Uses an event management mechanism.
- Single-threaded demultiplexing: A single thread (
Reactor) waits for events on a set ofhandles(e.g., sockets, file descriptors). - Event handlers: Objects that encapsulate the processing logic for each event.
-
Applicability: High-performance servers, event loops for GUIs, and notification systems.
-
Limitations and Challenges: Can become complex for managing intricate states. The
event handlerlogic must be very fast to avoid blocking the main thread. -
Relationship with Other Patterns:
- The Reactor pattern is often contrasted with the Proactor pattern.
Reactorhandles readiness events (e.g., “data is ready to be read”), whileProactorhandles completion events (e.g., “the read operation has completed”). - It implements the Observer pattern, where the
Reactorobserves multiple event sources (handles) on behalf of the application. - It is often used in conjunction with the Acceptor-Connector pattern to handle connection and service initialization.
- The Reactor pattern is often contrasted with the Proactor pattern.
Proactor
-
Problem: How to efficiently manage multiple long-running asynchronous operations in a non-blocking manner without using active polling?
-
Synopsis: The
Proactorpattern delegates the execution of long-running operations to separate threads or components. It notifies acompletion handleronce the operation is done, which is the inverse of theReactor.sequenceDiagram participant Client participant Initiator participant Proactor participant OS_Kernel participant CompletionHandler Client->>Initiator: Invoke async_operation(callback) Initiator->>OS_Kernel: Start async I/O operation (non-blocking) OS_Kernel-->>Initiator: Immediate return (operation in progress) Note over OS_Kernel,Proactor: Long-running I/O operation finishes OS_Kernel->>Proactor: Notify completion (result) Proactor->>CompletionHandler: Dispatch completion event CompletionHandler->>Client: Process result with callback
This sequence diagram shows the asynchronous behavior of the Proactor pattern. It highlights how the Initiator begins a non-blocking I/O operation via the OS Kernel and returns immediately to the Client. Once the operation completes, the OS Kernel notifies the Proactor, which then dispatches a completion event to the CompletionHandler to process the result.
- Key Characteristics:
- Asynchronous operations: The operation is launched, and the function call returns immediately.
- Completion-based: Once the operation is finished, the system generates a “completion” event.
- Proactor event loop: A thread or
thread poolwaits for notification of operation completion.
- Applicability: Server architectures that manage intense I/O (e.g., file servers, databases).
- Limitations and Challenges: Depends on native OS support for asynchronous I/O operations. Implementation complexity is often higher.
- Relationship with Other Patterns:
- The Proactor pattern is the asynchronous counterpart to the Reactor pattern. While
Reactoris based on synchronous event demultiplexing,Proactoris based on asynchronous operation completion. - It is often used with the Acceptor-Connector pattern to handle asynchronous connection establishment and service processing.
- The callback-based nature of the
CompletionHandleris similar to how the Command pattern can be used to encapsulate a piece of work to be executed later.
- The Proactor pattern is the asynchronous counterpart to the Reactor pattern. While
Acceptor-Connector
-
Problem: How to separate the logic of establishing a connection (accepting/connecting) from the service’s processing logic itself?
-
Synopsis: This pattern facilitates code reuse by decoupling the connection mechanics from the service implementation. The
Acceptorcreates a servicehandlerthat manages the communication.classDiagram class Acceptor { +accept() +createServiceHandler() } class Connector { +connect() +createServiceHandler() } class ServiceHandler { +handleService() } class Client { +requestConnection() } class Server { +startAccepting() } Client --o Connector : uses Server --o Acceptor : uses Acceptor --o ServiceHandler : creates Connector --o ServiceHandler : creates ServiceHandler <-- Client : communicates withThis class diagram illustrates the structural separation of roles in the
Acceptor-Connectorpattern. It clearly shows how theAcceptorandConnectorcomponents are responsible for connection management. TheAcceptoris used by theServerfor passive connections, while theConnectoris used by theClientfor active connections. Both roles ultimately delegate the service logic to aServiceHandlerinstance. -
Key Characteristics:
- Separation of concerns: Separates the roles of the
Acceptorand theConnectorfrom the servicehandler. - Acceptor: Manages the passive acceptance of client connections.
- Connector: Manages the active establishment of connections.
- Separation of concerns: Separates the roles of the
-
Applicability: Middleware frameworks, peer-to-peer architectures, client-server systems.
-
Limitations and Challenges: Can introduce some complexity if the connection logic is very simple.
-
Relationship with Other Patterns:
- The Acceptor-Connector pattern is almost always used with either the Reactor or Proactor pattern. The
AcceptororConnectorcreates aServiceHandler, which is then registered with theReactorto handle I/O events. - It is also an example of the Separation of Concerns principle, as it decouples the connection logic from the application-level service logic.
- The Acceptor-Connector pattern is almost always used with either the Reactor or Proactor pattern. The
Active Object
-
Problem: How to manage concurrency using an object-oriented approach, synchronizing access to shared data and separating a method’s invocation from its execution?
-
Synopsis: An
Active Objectencapsulates a control thread and a queue. Clients interact with it asynchronously, which protects the object’s internal state from concurrent access.sequenceDiagram participant Client participant Proxy participant Scheduler participant MethodRequestQueue participant ActiveObject_Impl participant ServantThread Client->>Proxy: invoke_async_method(args) Proxy->>MethodRequestQueue: add(MethodRequest(method, args)) Proxy-->>Client: Return immediately (future/promise) loop Scheduler loop Scheduler->>MethodRequestQueue: retrieve_next_request() activate Scheduler alt Request available Scheduler->>ActiveObject_Impl: execute(method, args) ActiveObject_Impl->>ServantThread: perform actual work else No request Scheduler->>Scheduler: Wait for requests end deactivate Scheduler endThis sequence diagram depicts the asynchronous behavior of the
Active Objectpattern. It shows how theClientmakes an asynchronous call on aProxy. TheProxyserializes the request into aMethodRequestQueue. A separateSchedulerandServantThreadthen retrieve and execute the request, ensuring concurrent access is managed safely. -
Key Characteristics:
- Asynchronous method invocation: Method calls are asynchronous.
- Method request queue: A queue stores method requests.
- Scheduler: An object that executes requests from the queue.
- Proxy: An object that manages the
Active Object’s interface and places requests into the queue.
-
Applicability: Systems that require concurrent or parallel execution and thread management.
-
Limitations and Challenges: Can introduce communication overhead due to the queues.
-
Relationship with Other Patterns:
- The Active Object pattern combines several other patterns to achieve concurrency. It uses a Proxy to provide an asynchronous interface to clients.
- The
MethodRequestQueueis a form of Message Queue, and theScheduleracts as a consumer in a Producer-Consumer relationship. - Each
MethodRequestis an implementation of the Command pattern, encapsulating a method call as an object.
Resilience and fault tolerance
graph TD A2_C(Resilience and Fault Tolerance); A2_C --> C1(Circuit Breaker); A2_C --> C2(Bulkhead); A2_C --> C3(Retry); A2_C --> C4(Timeout);
Circuit Breaker
- Problem: How to prevent a network or service failure from cascading to other parts of the system?
- Synopsis: The Circuit Breaker pattern is a stateful pattern that wraps a protected function call (typically a network call) in a circuit breaker object, which monitors for failures. It acts as a proxy for operations that might fail. For a detailed explanation of its implementation, states, and relationship with other resiliency patterns, see the main page on the Circuit Breaker Pattern.
Bulkhead
- Problem: How to prevent a failure in one part of a system from cascading and bringing down the entire system?
- Synopsis: The Bulkhead pattern isolates elements of an application into separate resource pools (e.g., thread pools, connection pools) so that if one component fails, it only exhausts its own pool, preventing the failure from affecting other parts of the system. For a detailed explanation of its implementation, types, and relationship with other resiliency patterns, see the main page on the Bulkhead Pattern.
Retry
-
Problem: How to handle temporary failures when communicating with remote services without immediately failing the operation?
-
Synopsis: The Retry pattern enables an application to handle transient failures by transparently retrying a failed operation. This pattern is particularly useful in distributed systems where temporary network issues or service unavailability are common. The core idea is to retry the operation a certain number of times with a delay between each attempt.
sequenceDiagram participant Client participant Service Client->>Service: Attempt 1 Service-->>Client: Fail (transient error) note over Client: Wait (backoff strategy) Client->>Service: Attempt 2 Service-->>Client: Fail (transient error) note over Client: Wait (increased backoff) Client->>Service: Attempt 3 Service-->>Client: Success -
Key Characteristics:
- Transient Fault Handling: Specifically designed to handle temporary issues like network glitches or short-lived service outages.
- Backoff Strategy: To avoid overwhelming a struggling service, a backoff strategy (e.g., exponential backoff) is used to introduce a delay between retries.
- Idempotency: The pattern should only be applied to operations that are idempotent, meaning they can be repeated multiple times without changing the outcome.
- Configurable: The number of retry attempts and the backoff strategy should be configurable.
-
Applicability:
- Unreliable Network Communications: When calling services over a network that may experience intermittent connectivity issues.
- Throttled Services: When a service may temporarily reject requests due to rate limiting.
- Database Connections: When there are transient issues with database connections or deadlocks.
-
Limitations and Challenges:
- Non-transient failures: The pattern is not suitable for non-transient (permanent) failures. Retrying a permanent failure will only waste resources.
- Retry Storms: If not implemented carefully with a backoff strategy, multiple clients retrying at the same time can create a “retry storm” that overwhelms the service.
- Latency: Retrying an operation adds latency to the overall response time.
Relationship with other patterns:
- Circuit Breaker: The Retry pattern is often used in conjunction with the Circuit Breaker pattern. After a certain number of retries, the circuit breaker can trip to prevent further calls to a failing service.
- Timeout: A Timeout pattern is essential to prevent a retry from waiting indefinitely for a response. Each retry attempt should have a timeout.
- Exponential Backoff: This is a common strategy used with the Retry pattern to increase the wait time between retries, giving the service time to recover.
Timeout
-
Problem: How to prevent an application from waiting indefinitely for a response from a remote service that may be slow or unresponsive?
-
Synopsis: The Timeout pattern sets a time limit on how long an application will wait for a response to a request. If no response is received within the time limit, the operation is considered to have failed. This prevents resources from being tied up waiting for a response that may never come.
sequenceDiagram participant Client participant Service Client->>Service: Request note over Client: Start timer alt Response received in time Service-->>Client: Success else Timeout note over Client: Timer expires Client->>Client: Handle timeout (e.g., fail operation) end -
Key Characteristics:
- Time Limit: A predefined duration to wait for a response.
- Fail-Fast: If the time limit is exceeded, the operation fails immediately.
- Resource Protection: Prevents system resources (like threads) from being blocked indefinitely.
- Improved User Experience: Provides quicker feedback to the user when a service is unresponsive.
-
Applicability:
- Remote Service Calls: Any interaction with an external service over a network.
- Database Queries: To prevent long-running queries from blocking the application.
- Resource-intensive operations: Any operation that has the potential to hang.
-
Limitations and Challenges:
- Choosing the right timeout value: Too short, and it might fail operations that are just slow. Too long, and it defeats the purpose of the pattern. The value often needs to be configurable and sometimes dynamically adjusted.
- Cascading Timeouts: In a chain of service calls, the timeout of the calling service should be shorter than the timeout of the called service to get a meaningful error.
Relationship with other patterns:
- Retry: The Timeout pattern is often used with the Retry pattern. Each retry attempt should have its own timeout.
- Circuit Breaker: A timeout can be a signal that a service is failing. The Circuit Breaker can count timeouts as failures and trip if the threshold is exceeded.
- Bulkhead: Timeouts can help prevent a slow service from consuming all the threads in a bulkhead.
Coordination
graph TD A2_D(Coordination); A2_D --> D1(Leader Election);
Leader Election
The Leader Election pattern is a fundamental process in distributed systems for dynamically designating a single instance as a leader to coordinate tasks and prevent conflicts. For a detailed explanation of its implementation, use cases, and challenges, see the main page on the Leader Election Pattern.
Deployment and Infrastructure
graph TD A2_E(Deployment and Infrastructure); A2_E --> E1(Sidecar); A2_E --> E2(Ambassador);
Sidecar
- Problem: How to add additional functionality (like logging, monitoring, or security) to an existing application component without modifying its code?
- Synopsis: The Sidecar pattern attaches a separate, helper process or container to a main application to provide supporting features and abstract away infrastructure concerns. For a detailed explanation of its use cases, implementation in Kubernetes, and trade-offs, see the main page on the sidecar pattern.
Ambassador
- Problem: How can an application offload and standardize the way it communicates with external services, especially when dealing with concerns like resiliency, routing, and monitoring?
- Synopsis: The Ambassador pattern is a specialized variant of the sidecar pattern that specifically handles outbound network requests on behalf of an application. It acts as a co-located proxy that abstracts the complexities of remote communication. For a detailed explanation, see the section on the Ambassador Pattern.
Other POSA Patterns
These patterns belong to other categories (architectural, etc.) but are also fundamental elements of software architecture.
Architectural Patterns
Idioms
Idioms are low-level patterns, often specific to a programming language (like C++ or Java), that describe how to solve fundamental implementation problems. Unlike design patterns, which focus on object structure and collaboration, idioms are closer to the code itself.
-
Handle-Body: Separates an object’s interface (the
Handle) from its implementation (theBody). Clients only interact with theHandle, which helps to hide implementation details, reduce compilation dependencies, and simplify memory management. It is a form of the “Pimpl” (Pointer to Implementation) idiom. -
Counted Body (or Counted Pointer): A memory management idiom where multiple
Handleobjects share the sameBody. TheBodymaintains a reference count. When aHandleis destroyed, the count is decremented. TheBodyis only destroyed when the count reaches zero. This is the basic principle behind smart pointers likestd::shared_ptrin C++. -
…
Resources & links
Videos
In this video by Douglas Schmidt, the section starting at [00:06:45] focuses on the patterns from the POSA 1 book. It explains how these patterns are structured, differentiating between architectural patterns and design patterns. The creator also discusses specific POSA 1 patterns such as Broker and Command Processor, and highlights the relevance of many POSA patterns for concurrent and networked software, including Publisher-Subscriber and Layers.