.NET Architecture Weekly #3

Introduction

.NET Architecture Weekly is a journal about learning software architecture on .NET and not just limited* to .NET only.
PS: Why? Because software architecture designs normally can use in any languages or frameworks :).

Week #3

1) Microservices Architecture Design Patterns – Part 1

What are Microservices Architecture Design Patterns?

Microservices Architecture Design Patterns are some of the common patterns that are used to solve common problems that arise when designing and developing microservices. Some of them can be found in those big tech companies that use a lot of microservices such as Netflix architecture. They help to address challenges such as service discovery, communication, scalability, reliability, security, and data consistency.

I) Service Registry Pattern

Service Registry / Discovery Pattern is a microservice architecture pattern that helps to manage service discovery. It is used to keep track of the location of services and their instances in a distributed system, so we do not need hardcode or maintain the instances url of a service ourselves. You can read What is Eureka Service Discovery in Java and .NET? by clicking the link.

---
title: Service Discovery Pattern
---
graph RL
  SA[Service A]
  SR([Service Registry])
  SA -->|1.Register & 2.Query Service B| SR
  SB1 -->|1.Register| SR
  SB2 -->|1.Register| SR
  SR -->|3.Return Instance1| SA
  SA -->|4.Rest call| SB1

  subgraph Service B
    SB1(Instance1)
    SB2(Instance2)
  end

II) Circuit Breaker Pattern

The Circuit Breaker pattern is a design pattern used in microservices architecture to create resilient and responsive services by limiting the impact of some instance’s failures, latencies and hence can prevent cascading failures and help keep the application alive. It acts as a proxy between the service and its clients and can switch between three states after health check: closed (normal), open (unavailable), and half-open (under observation and partially OK).

---
title: Circuit Breaker Pattern
---
flowchart LR
    id1(Service) --> id2(Circuit Breaker)
    id2 -->|Closed| id3(Client)
    id2 -->|Open| id4(Fallback)
    id2 -->|Half-Open| id5(Probe)
    id3 <-->|Health Check OK| id2
    id4 x--x|Health Check Failed| id2
    id5 --x|Health Check Partially Failed| id2

    style id3 fill:#0f0
    style id4 fill:#f00
    style id5 fill:#ff0

III) SAGA Pattern

SAGA pattern is a design pattern used in microservices architecture to manage complex transactions that involve multiple services. It provides a way to handle data consistency in distributed systems and rollback transaction if there is any failure on any step.

---
title: SAGA Pattern
---
sequenceDiagram
    User-->Booking Service: Booking a seat
    break when the booking process fails
        Booking Service-->>User: Cancel booking
    end
    Booking Service-->>Reservation Service: Reserve seat
    break when the reserve process fails
        Reservation Service-->>Booking Service: Reverse seat
        Booking Service-->>User: Cancel booking
    end
    Reservation Service-->>Payment Service: Process payment
    break when the payment process fails
        Payment Service-->>Reservation Service: Reverse payment
        Reservation Service-->>Booking Service: Reverse seat
        Booking Service-->>User: Cancel booking
    end
    Payment Service-->>Notification Service: Send notification
    break when the notification process fails
        Notification Service-->>Payment Service: Cancel confirmation
        Payment Service-->>Reservation Service: Reverse payment
        Reservation Service-->>Booking Service: Reverse seat
        Booking Service-->>User: Cancel booking
    end

IV) Command Query Responsibility Segregation (CQRS) Pattern

CQRS is a microservice architecture pattern that separates read and write operations into two services. It can improve performance, scalability, and security of an application.

---
title: CQRS Pattern
---
graph TD
  Users -->|REST call| Command
  Users -->|REST call| Query
  subgraph Write
    Command -->|DB call|DB1[DB Write]
  end
  subgraph Read
    Query -->|DB call| DB2[DB Read]
  end
  DB1 -->|Eventual Consistency| DB2

  style Users fill:#f0b37e
  style Command fill:#a9dc76
  style Query fill:#a9dc76
  style DB1 fill:#78dce8

V) Bulkhead Pattern

The bulkhead pattern is a microservice architecture pattern that partitions different parts of an application into isolated pools. This helps to contain failures, attacks or performance degradations from spreading to other parts of the system.

---
title: Bulkhead Pattern
---
graph TD
  subgraph Workload and Connection Pool
    Workload1 --x CPA[Connection Pool]
    Workload2 --> CPB[Connection Pool]
    Workload2 --> CPC[Connection Pool]
  end
  CPA --xSVA[Service A]
  CPB -->SVB[Service B]
  CPC -->SVC[Service C]

style Workload1 fill:#ff0000,color:#fff
style CPA fill:#ff0000,color:#fff
style SVA fill:#ff0000,color:#fff

style Workload2 fill:#00ff00
style CPB fill:#00ff00
style CPC fill:#00ff00
style SVB fill:#00ff00
style SVC fill:#00ff00

References:
View at Medium.com
https://levelup.gitconnected.com/12-microservices-pattern-i-wish-i-knew-before-the-system-design-interview-5c35919f16a2
https://www.edureka.co/blog/microservices-design-patterns
https://blog.bitsrc.io/how-to-use-saga-pattern-in-microservices-9eaadde79748
View at Medium.com
https://learn.microsoft.com/en-us/azure/architecture/patterns/bulkhead

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.