EVENT BASED MICROSERVICES - Event Sourcing
In a Microservice Architecture, especially with Database per Microservice, the Microservices need to exchange data. For resilient, highly scalable, and fault-tolerant systems, they should communicate asynchronously by exchanging Events. In such a case, you may want to have Atomic operations, e.g., update the Database and send the message. If you have SQL databases and want to have distributed transactions for a high volume of data, you cannot use the two-phase locking (2PL) as it does not scale. If you use NoSQL Databases and want to have a distributed transaction, you cannot use 2PL as many NoSQL databases do not support two-phase locking. In such scenarios, use Event based Architecture with Event Sourcing. In traditional databases, the Business Entity with the current “state” is directly stored. In Event Sourcing, any state-changing event or other significant events are stored instead of the entities. It means the modifications of a Business Entity is saved as a series of immutable events. The State of a Business entity is deducted by reprocessing all the Events of that Business entity at a given time. Because data is stored as a series of events rather than via direct updates to data stores, various services can replay events from the event store to compute the appropriate state of their respective data stores.Pros
- Provide atomicity to highly scalable systems.
- Automatic history of the entities, including time travel functionality.
- Loosely coupled and event-driven Microservices.
Cons
- Reading entities from the Event store becomes challenging and usually need an additional data store (CQRS pattern)
- The overall complexity of the system increases and usually need Domain-Driven Design.
- The system needs to handle duplicate events (idempotent) or missing events.
- Migrating the Schema of events becomes challenging.
When to use Event Sourcing
- Highly scalable transactional systems with SQL Databases.
- Transactional systems with NoSQL Databases.
- Highly scalable and resilient Microservice Architecture.
When not to use Event Sourcing
- Lowly scalable transactional systems with SQL Databases.
- In simple Microservice Architecture where Microservices can exchange data synchronously (e.g., via API).
Enabling Technology Examples
Event Store: EventStoreDB, Apache Kafka, Confluent Cloud, AWS Kinesis, Azure Event Hub, GCP Pub/Sub, Azure Cosmos DB, MongoDB, Cassandra, Amazon DynamoDB.
Frameworks: Lagom, Akka, Spring, akkatecture, Axon, Eventuate
Comments
Post a Comment