Showing posts with label MicroServices. Show all posts
Showing posts with label MicroServices. Show all posts

Friday, November 29, 2024

Micro Services - Backward compatibility design

Microservice Backward Compatibility

In a microservices architecture, ensuring backward compatibility is crucial for maintaining system stability and allowing independent deployment of services without breaking upstream dependencies.

One of the primary goals in a microservices architecture is to enable independent deployment of services. This means that changes to one service should not break or disrupt the functionality of other services that depend on it. To achieve this, backward compatibility must be maintained, ensuring that new changes do not affect existing contracts or interfaces. 

Here are some key approaches to achieve backward compatibility: 

Approach: Contract Models – Enhance Instead of Change

When evolving a microservice, it is important to enhance the contract models rather than changing them. This means adding new fields or endpoints instead of modifying or removing existing ones. By doing so, existing clients can continue to function without any changes, while new clients can take advantage of the enhanced features. This approach ensures that the service remains backward compatible and does not break any upstream dependencies. 

Approach: Create Adaptor

Another effective approach is to create an adaptor. Instead of changing the contract model directly, an adaptor can be introduced to handle the differences between the internal business model and the contract model. This allows the internal business model to evolve independently of the contract model. The adaptor translates between the two models, ensuring that the contract remains stable and backward compatible. This approach provides flexibility to change the internal business logic without impacting the external contract. 

Version Strategy to Communicate Major Changes

When major changes are necessary, a versioning strategy should be employed. Versioning the API allows clear communication of the contract version that the API is working with. By using version numbers in the API endpoints (e.g., /api/v1/resource), clients can explicitly specify which version of the contract they are using. This ensures that clients are aware of the changes and can migrate to the new version at their own pace, while still maintaining backward compatibility with the older versions.

 


Micro Services - Resiliency

In modern software systems, resiliency is a critical aspect that ensures the system remains functional and responsive even in the face of failures or unexpected conditions. Several design patterns can be employed to enhance the resiliency of a system. Below are some key patterns: 

Timeout Pattern

The Timeout pattern is used to configure a timeout for all downstream calls. This pattern helps in failing fast by setting a maximum time limit for a call to complete. If the call does not complete within the specified time, it is aborted. This prevents the system from waiting indefinitely for a response, which can lead to resource exhaustion and degraded performance.  However, if there are a lot of calls within this timeout period, it can still lead to issues. This is where the Circuit Breaker pattern comes into play. 

Circuit Breaker Pattern

The Circuit Breaker pattern is often used in conjunction with the Timeout pattern. It involves implementing a circuit breaker component that tracks all outgoing calls from the service. If the component observes a high number of failures (above a certain threshold), it transitions to an "open" state. In this state, the circuit breaker immediately responds with an error message or a default response, without attempting to make the call. This prevents the system from being overwhelmed by repeated failures and helps maintain overall system health by avoiding cascading failures.  The circuit breaker also has a "half-open" state, where it allows a few calls to pass through to check if the downstream services have recovered. If the calls succeed, the circuit breaker transitions back to the "closed" state, allowing normal operation to resume. 

Retry Pattern

The Retry pattern is useful for handling transient issues. It involves retrying a failed operation a certain number of times before giving up. This pattern helps in self-correcting the services and is particularly effective when used with the Circuit Breaker and Timeout patterns. By retrying operations, the system can recover from temporary issues without manual intervention. 

Bulkhead Pattern

The Bulkhead pattern involves separating services by their criticality and functionality. High-criticality services are allocated more resources to ensure their availability. This separation makes it easier to manage and segregate execution. Additionally, workload balancing can be used to distribute the load across multiple instances of a service. Load shedding can also be employed, where the load balancer redirects requests from overloaded instances to less busy ones. This ensures that retries are more likely to succeed by avoiding overloaded instances. 

Caching Pattern

The Caching pattern involves storing responses for repeated data to reduce the load on the system. By caching frequently requested data, the system can serve responses faster and reduce the number of calls to downstream services. This not only improves performance but also enhances resiliency by reducing the dependency on external services.

 

Micro Services - Database Design Pattern

In a microservices architecture, each microservice typically manages its own database. This approach offers several benefits, such as improved scalability and independence. However, it also introduces challenges, particularly around data sharing and consistency. To address these challenges, several complementary patterns can be employed: 

Event-Driven Pattern

In an event-driven architecture, each microservice saves data in its own database. The limitation of this approach is the difficulty in sharing data between microservices. To overcome this, microservices can connect with each other and create a copy of data from other microservices, storing it as a local cache. This allows microservices to access the necessary data without directly querying another service's database, thus reducing inter-service dependencies and improving performance. 

Event Sourcing Pattern

Event sourcing involves storing data as a series of events. This pattern is straightforward to implement in a monolithic database but is also feasible with microdatabases. Instead of storing the current state of an entity, all changes (events) to the entity are stored. To determine the latest state of the data, all events must be replayed and evaluated. While this approach can impact performance and increase storage requirements, it offers the advantage of easy data rollback by replaying events up to a certain point. Event sourcing provides a detailed audit trail and can be useful for debugging and compliance purposes. 

CQRS (Command and Query Responsibility Segregation) Pattern

CQRS separates the reading and writing of data into different models. In this pattern, there are distinct services for handling commands (writes) and queries (reads). The write and read databases are different, and a synchronization mechanism is required to keep them in sync. This separation allows for optimized read and write operations, as each can be scaled and optimized independently. However, ensuring data consistency between the write and read databases is critical and can add complexity to the system.

Micro Services - Asynchronous design

Asynchronous communication is a key aspect of modern microservices architecture, providing several benefits that enhance the overall system performance and scalability :

De-coupling Across Components - Asynchronous communication decouples components, allowing them to operate independently without waiting for each other. This reduces dependencies and improves system resilience. 

Fire-and-Forget Interaction- In a fire-and-forget interaction, the sender sends a message and does not wait for a response. This is useful for tasks that do not require immediate feedback, reducing the load on the system and improving responsiveness. 

Support Long-Running Jobs - Asynchronous communication supports long-running jobs by allowing tasks to be processed in the background. This prevents blocking resources and ensures that the system remains responsive to other requests. 

 

Microservice Functional Requirements

To build effective microservices, certain functional requirements must be met: 

Loosely Coupled Service  - Microservices should be loosely coupled, meaning changes in one service should not impact others. This enhances maintainability and scalability. 

Backward Compatibility - Independently Changeable - Services should be independently changeable without breaking existing functionality. This ensures that updates can be made without disrupting the system. 

Backward Compatibility - Independently Deployable - Microservices should be independently deployable, allowing for updates and deployments without affecting other services. 

Support and Honor Contracts - Microservices must support and honor contracts, ensuring consistent and reliable communication between services. 

Technology Agnostic API - APIs should be technology agnostic, allowing different technologies to interact seamlessly. 

Stateless - Microservices should be stateless, meaning they do not retain client state between requests. This simplifies scaling and improves reliability. 

Lightweight Communication - Communication between microservices should be lightweight to reduce overhead and improve performance. 

Cache-able Communication - Communication should be cache-able to improve efficiency and reduce redundant processing. 

Usability – API Consistency, Predictable, Readable - APIs should be consistent, predictable, and readable to enhance usability and developer experience. 

Usability – Query-able Data - APIs should allow for query-able data to enable flexible and efficient data retrieval. 


Pragmatic REST API - Pragmatic REST API refers to a more practical approach to REST, focusing on simplicity and usability rather than strictly adhering to CRUD operations. 

Example :

Use Verbs Instead of Nouns: Use verbs in the API to denote actions, e.g., /startProcess instead of /process.

Append Query Parameters: Use query parameters to pass constraints, e.g., /search?query=example.

HATEOAS: Implement Hypermedia as the Engine of Application State (HATEOAS) by including URLs in responses to guide the client on the next steps, e.g., the server responds with the next URL needed for the client to make a call.

By following these principles, microservices can achieve better performance, scalability, and maintainability, while providing a more practical and user-friendly API.

 

Facade Pattern

The Facade pattern is popular for keeping implementation separate from the contract. It provides flexibility in the implementation without impacting the integration. This pattern simplifies the interface for the client and hides the complexities of the underlying system. 

Proxy Pattern

The Proxy pattern is popular when connecting with other services. The proxy object helps in various scenarios such as caching and making the internal model extendable. It can also assist with authentication and authorization. Additionally, the proxy pattern is beneficial for unit testing, as mocking proxy data is straightforward.

Micro Services - synchronize data

 Data Consistency Across Microservices

In a distributed microservices architecture, ensuring data consistency is crucial. When a failure occurs in one step, other transactions must either be canceled or rolled back to maintain consistency. 

 

Traditional Approach - Traditionally, all updates are made as a single transaction, ensuring that either all operations succeed or fail together. This approach is feasible in monolithic architectures where a single database can be used to update all records simultaneously. However, this is not possible with microservices, which typically use distributed databases, each maintaining its own data. 

 

ACID Properties

Transactions in microservices should adhere to ACID properties: 

Atomicity: All or nothing when committing data changes.

Consistency: Data transitions from one valid state to another.

Isolation: Transactions run in isolation and are not affected by concurrency.

Durability: Committed data changes are durable and persist in the database.

 

2-Phase Commit Pattern

The 2-Phase Commit pattern focuses on data consistency rather than availability. It introduces a transaction manager (TxnMgr) to ensure data consistency. The process involves: 

Event Publication: The UI/BFF publishes an event to the bus.

Transaction Manager: TxnMgr subscribes to the event and creates a record in its database, detailing the components involved in the transaction.

Prepare Event: TxnMgr publishes a "prepare" event, which other microservices subscribe to and prepare for the task.

Vote Collection: Microservices publish their vote (success/fail) after preparation. TxnMgr records these votes.

Commit/Rollback: If all votes are successful, TxnMgr publishes a "commit" event, and microservices complete the task. If any service fails, TxnMgr publishes a "rollback" event, and microservices roll back their preparation steps.

Pros: 

    Guaranteed atomicity across services.

    ACID compliant.

Cons: 

    Performance bottleneck due to waiting for TxnMgr responses.

    Single point of failure with TxnMgr.

    Complexity in handling retries and failures.

 

Saga Pattern

The Saga pattern focuses on atomicity and is more popular than the 2-Phase Commit pattern. It uses a Saga Execution Coordinator (SEC) and saga logs to manage transactions. The process involves: 

Saga Entries: Incoming transactions are converted into saga entries, each representing a service task with a corresponding compensation request (rollback request).

Status Recording: SEC records the status of each microservice. If all responses are complete, the transaction is complete.

Communication: Requests can be sent to individual services together or one-by-one using HTTP or events.

 

Pros: 

    Ensures data integrity.

    Fault tolerance with compensation requests.

    Scalable and loosely coupled.

Cons: 

    Implementation complexity.

    Performance overhead due to asynchronous communication and compensating actions.


Eventual Consistency Pattern

The Eventual Consistency pattern prioritizes availability over strict ACID properties. Asynchronous messages are published, and microservices process them independently. Data may be temporarily out of sync, but it will eventually become consistent.  

Pros: 

    High availability.

    Simple implementation.

Cons: 

    Temporary data inconsistency.

    Requires careful handling to ensure eventual consistency.

Micro Services - Composition


Microservices architecture often requires composing multiple services to complete a single operation. Here are various composition patterns used in microservices: 

When an operation requires more than one service to complete, a composition pattern is used. This involves coordinating multiple microservices to achieve a single business goal. 

 

Broker Composition Pattern

In the Broker Composition pattern, a message is published to a message broker. All subscribers pick up this message and process their tasks. Once all the work is complete, the client gets a notification and can pull the relevant data back. This asynchronous communication provides flexibility, good performance, and reliability. 

 

Aggregate Composition Pattern

The Aggregate Composition pattern involves an aggregator component, often a Backend for Frontend (BFF), responsible for fetching data from multiple microservices. The aggregator connects with other microservices, aggregates the data, and returns it to the front end. However, synchronous calls between the aggregator and other microservices can lead to a poor client experience. 

 

Chained Composition Pattern

The Chained Composition pattern is considered an anti-pattern. In this pattern, the client calls Service 1, which calls Service 2, and so on, creating a chain of HTTP calls. This can lead to increased latency and complexity. 

 

Proxy Composition Pattern

In the Proxy Composition pattern, an API gateway connects with all downstream services and returns the information to the client. The gateway does not aggregate data; instead, the front end is responsible for making multiple gateway calls and aggregating the data. These endpoints are called passive endpoints because the proxy has minimal logic and acts as a pass-through. The API gateway can provide benefits such as a centralized layer for security checks and data caching. It can also handle API version mapping. However, the drawback is that the client has to make multiple calls, one for each service. 

 

Mixed Composition Pattern

The Mixed Composition pattern combines multiple composition patterns. It uses the Broker Composition pattern as the base and adds a BFF as an aggregator layer. Once a message is published to the broker and a notification indicates that the data is ready, the aggregator fetches the data points, aggregates them, and returns them to the client. The BFF can connect with individual services to get the data, avoiding a chain of calls. An API gateway (Proxy Composition pattern) can be introduced on top of the services to expose the data to more consumers.

 


Micro Services - Architect with context

Begin with Microservices

 

Traditionally, software development has often relied on monolithic architectures, where all functionalities are built as part of a single, large service. This approach can lead to significant issues, as making a change in one aspect of the system can impact the entire product. Microservices architecture addresses this problem by creating boundaries around different aspects of the product, known as contexts. Each context is implemented as an independent service, referred to as a bounded context. 

 

Bounded Context

A bounded context is a specific area of the application with a clear boundary and interface. Each microservice has a specific role centered around this context. Bounded contexts can have sub-contexts, which may be related to data/models or functions. There are two primary approaches to defining bounded contexts: 

 

Ubiquitous Language: 

This approach is suitable for smaller projects and teams. The bounded context is defined based on the language or context used within the team. During the definition process, if overlapping sub-contexts are identified, they are either renamed to match the bounded context or extracted to form a new bounded context. If cross-references between bounded contexts remain, these points become integration points.

 

Event Storming: 

Event storming is a planning, estimation, and design session where the team goes over the details of all the events that form the product. This includes the sequence of events, associated commands (which trigger events), and any issues related to the events. By aggregating all the notes around these events, the team can define the bounded context and the corresponding services.

 

Aggregation of Microservices

In some cases, the contexts of two services are so closely related that creating a single service is more efficient. In such scenarios, an aggregate service is created to encompass both contexts.

 

Understanding Architecture

In a microservices architecture, asynchronous communication is essential to ensure scalability, responsiveness, and efficient resource utilization. Here’s a detailed look at why asynchronous connections are needed and how to implement them effectively. 

 

Why We Need Asynchronous Connections

With microservices architecture, numerous components are connected over the network. To complete a particular action, multiple microservices may need to interact. Keeping synchronous calls between services can lead to a poor user experience and cause more network resources to be blocked while waiting for responses. Asynchronous connections decouple clients and services, eliminating direct dependencies and improving overall system performance. 

 

Asynchronous Architecture

 

Work Queue Pattern

In the Work Queue pattern, multiple workers are available to finish a given task. Once a task is provided, any available worker executes it. Each piece of work is independent and can be processed by separate workers. This pattern allows scaling up workers to handle the load without impacting the outcome. The caller/publisher gives the message to an individual broker, where multiple workers are waiting to act upon it. 

 

Publish and Subscribe Pattern

The Publish and Subscribe pattern differs from the Work Queue pattern. Here, the publisher publishes an event, and subscribers subscribe to it based on their responsibilities. This pattern is more event-driven rather than command-driven. Multiple tasks by multiple workers are performed when one event is raised. 

 

Asynchronous API Calls

Messaging is not the only way to achieve asynchronous communication. Asynchronous communication can also be implemented using HTTP APIs. In this pattern, the front end talks to the BFF and receives an immediate response. The BFF then communicates with downstream services and provides a callback. Once the downstream service completes its work, it uses the callback to notify the BFF, which in turn notifies the UI that the response is available.