Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Event-Driven Architecture (EDA)

Definition:
EDA is a design paradigm in which software components perform actions in response to events.

Components:
Event Producers: Generate events and send them to the message broker.

Message Broker (or Event Bus): Mediates the delivery of events from producers to consumers. Examples
include Kafka, RabbitMQ, and Azure Event Hub.

Event Consumers: Listen to specific events and perform actions when they occur.

Events: Notifications indicating that something has occurred. They typically contain data about the
occurrence.

Event Store: An optional but useful component for storing events, especially when implementing Event
Sourcing.

Problem it Solves:
EDA decouples components so they can evolve independently, improves scalability, and allows for more
flexible system behaviors.

Command Query Responsibility Segregation (CQRS)


Definition:
CQRS is a pattern where the data write responsibilities (commands) and data read responsibilities
(queries) are separated into different objects or models

Components:
Command Model: Responsible for handling commands which mutate the state. This can involve
validation, business logic, and persistence.

Query Model: Responsible for handling queries. This might be optimized for read performance and can
be denormalized.

Command: Represents an intention to change state. It can be as simple as a method call with
parameters.

Event Store: In systems combining CQRS with Event Sourcing, changes are stored as a series of events
rather than state changes.
Problem it solves:
Scalability: Read and write workloads can be scaled independently.

Flexibility: The system can be optimized separately for reading and writing.

Complexity Management: By segregating responsibilities, the domain logic for writes can be kept
separate from the logic for reads.

Use Cases for CQRS:


Scalable Systems: When a system experiences a heavy load, CQRS allows read and write operations to
scale independently.

Complex Domain Logic: In domains where the business logic for write operations is complex, separating
reads from writes can simplify the codebase.

Event Sourcing: If a system is storing changes as events (Event Sourcing), then CQRS is a natural fit, as
the events can be replayed to build the current state.

Reporting: When the system requires intensive reporting, the read side can be optimized without
affecting the write side.

Distributed Systems: In Microservices or other distributed architectures, CQRS can help reduce the
contention and coupling between services.

It's worth noting that while EDA and CQRS can be used separately, they often go hand in hand. For
example, after executing a command in a CQRS system, an event might be produced to notify other
parts of the system about the change, leading to an event-driven flow.

Problem:
Credit Card Processing and Payment System
As the customer base grows and transaction volumes increase, enterprise running into several
challenges:

Performance Bottleneck: During peak transaction times, such as Black Friday or Cyber Monday, their
system experiences latency, especially when simultaneous charges and payments are being processed.
Consistency Issues: Sometimes, the balance shown to the customer is not updated in real-time, leading
to over-limit charges or other errors.

Complexity: With regulatory requirements and the need for sophisticated fraud detection, the
processing logic is becoming increasingly complex.

Solution Pattern:
Event-Driven Architecture + CQRS
Event-Driven Architecture:

 Transactions, whether it's a charge or payment, generate events.


 These events are asynchronously processed, allowing for a non-blocking system.

CQRS:

 The system is segregated into a Command side (actions like charging or making payments) and a
Query side (actions like checking balance or transaction history).

Solution:
Command Side (Write Model):

 When a customer makes a purchase using the credit card, a ChargeInitiated event is generated.
 This event is processed by the Transaction Service, which logs the charge and adjusts the
available credit.
 Similarly, when a payment is made, a PaymentInitiated event is generated and processed to
update the balance.
 For fraud detection, events can be sent to the Fraud Detection Service. If a potential fraud is
detected, a PotentialFraudAlert event can be generated.

Query Side (Read Model):

 The Balance Service provides real-time balance and transaction history for customers.
 This service has its own optimized database for reads. Events from the Command side (like
ChargeInitiated or PaymentInitiated) update this read model.
 This separation ensures that even during high-write scenarios (like massive sales days),
customers can still quickly and efficiently check their balances and statements.

Event Store:

 An immutable event store captures all transactional events.


 This not only acts as an audit trail (essential for financial companies) but can be used to rebuild
the state if needed.

Event Handlers & Additional Services:

 Several services can act as event handlers. For instance:


 A Notification Service can inform customers of charges, payments, or fraud alerts.
 A Reporting Service can generate financial reports for regulatory bodies based on these
events.

Benefits:
Scalability: EDA combined with CQRS allows the system to handle high volumes of transactions without
compromising on read operations like balance checks.

Resilience & Consistency: If any part of the system temporarily fails, it can recover by processing the
missed events from the event store. This ensures data consistency.

Flexibility & Extensibility: As regulations evolve or new features are required (like advanced fraud
detection techniques), new event handlers or services can be added without disrupting the core system.

Auditing & Compliance: The immutable event store provides a built-in audit trail, essential for financial
systems.

By adopting EDA and CQRS, a financial company build a resilient, scalable, and compliant system that
can handle the complexities and demands of modern credit card processing and payments.

You might also like