Event-Driven Architecture
Decoupled components communicate by publishing and reacting to events, rather than calling each other directly.
Context
Services that need to react to business facts without coupling to the producer.
Solution
Producers publish events (OrderPlaced, PaymentFailed) to a broker. Consumers subscribe.
// Producer
await _bus.PublishAsync(new OrderPlaced(order.Id, order.Total));
// Consumer (otro servicio)
public class SendConfirmationEmail : IConsumer<OrderPlaced>
{
public Task Consume(ConsumeContext<OrderPlaced> ctx) => /* enviar email */;
}When NOT to use it
- When the business requires an immediate synchronous response.
- Without idempotency and versioned schemas, the system becomes fragile.
Tradeoffs
| Pro | Con |
|---|---|
| Minimal coupling | Difficult to trace an end-to-end flow |
| Scales very well | Eventual consistency is mandatory |
#architecture #events