7 Eylül 2020 Pazartesi

Yazılım Mimarisi - Microservices Architecture Saga Örüntüsü - Transaction Yönetimi İçindir

Giriş
Microservice mimarisine girince mutlaka distributed transaction da oluyor. Açıklaması şöyle
The common distributed transaction design patterns are Saga, Command and Query Responsibility Segregation (CQRS), Domain Event, Event Sourcing, and Transactional Outbox.
Saga Örüntüsü Neden Lazım?
Birden fazla kaynağa dokunan bazı işlerde ACID özelliklerini kullanmak istersek Two Phase Commit  kullanılabilir. Ancak bazen Two Phase Commit kullanmak mümkün olmayabilir. Çünkü 

1. iş yapan kaynaklar Two Phase Commit'i desteklemeyebilirler. Açıklaması şöyle
- Modern NoSQL databases like MongoDB and Cassandra don’t support them.
- Modern message brokers like Apache Kafka don’t support them.
- Synchronous IPC reduces availability.
- All the participants must be available.
2. Two Phase commit uzun süren (Long-lived) işlemler için uygun değildir. Açıklaması şöyle.
It is not advisable to use two-phase transaction protocols to control long-lived transactions since the locking of resources for prolonged durations across trust boundaries is not practical, rather is not at all advisable.
Bu durumda Saga örüntüsüne başvuruyoruz. Yani Saga örüntüsü birden fazla kaynağın dahil olduğu,  uzun sürebilen ve ACID özelliklerinin gerekmediği işlemlerde kullanılır. Açıklaması şöyle
SAGA architecture pattern helps you to manage transaction across different microservices each having respective Database
Saga ve Outbox Pattern
Saga ile Outbox Pattern kullanılabilir. Açıklaması şöyle
While applying the Saga pattern, you will have two operations at each step. The local ACID transaction for business logic, and the event publishing.

These two operations cannot be in the same single unit of work as they target separate data sources. One is the local database, and the other is the event store. To perform these operations consistently, you can apply the Outbox pattern. The Outbox pattern relies on having a local outbox table to hold events in the same database where you run the local transactions for business logic. Then you can use these two database tables in the same transaction to perform local ACID transaction for business logic and event publishing. You can then read the events from the outbox table and publish them asynchronously
Unit Of Work Nedir
Açıklaması şöyle
The Unit Of Work design pattern is a design pattern that prevents every database-related action in our software application from being immediately reflected to the database. Instead, it accumulates all actions and makes sure they are completed all at once over a single connection, reducing database costs.

Saga ve CQRS
Saga ile CQRS kullanılabilir. Açıklaması şöyle
In CQRS, when the write operation is persisted, an event is stored in event-store. This event is used to update the read store asynchronously. Events can also be replayed multiple times according to requirements to create different types of query stores.

As the read store is updated asynchronously at a later time, it will result in an Eventual consistent system. Eventual consistency provides high availability and better performance by sacrificing the strong consistency. CQRS will also eliminate the requirement for distributed transactions if eventual consistency can be accepted.

Saga Gerçekleştirim Yöntemleri - Saga Coordination Strategies
Saga için kullanılabilecek 3 yöntem var.
1. Orchestration-Based Saga
2. Choreography-Based Saga

Saga Kullanan Örnek
Bir örnek şöyle.
The Product Owner would like to introduce a new feature — the ability to ship high volume orders. But there's a catch: high volume orders are too large to ship in a single shipment. We need to split them into batches. But, we only want to ship complete orders. This means that if we cannot ship one batch, we don't want to ship any batch.

The Saga pattern advocates splitting the big transaction (ship all batches) into smaller transactions (one per batch). But since these transactions are not isolated, we need to be able to compensate them:

1. Orchestration-Based Saga
Orchestration-Based Saga yazısına taşıdım.

2 Choreography-Based Saga
Choreography-Based Saga yazısına taşıdım.

Hiç yorum yok:

Yorum Gönder