Microservices & Event-Driven Architecture (EDA) Mastery

Idempotency: Preventing duplicate message processing

1 Views Updated 5/4/2026

Mastering Idempotency

In a distributed system, you will receive the same message twice. It's not a matter of 'if', but 'when'. Idempotency ensures that processing the same event multiple times has no additional effect.

1. Why do duplicates happen?

Usually because of network timeouts. Service A sends a message to Service B. Service B processes it but the "Ack" (Acknowledgment) gets lost. Service A thinks it failed and sends it again. Without idempotency, you might charge the user's card twice!

2. Implementation Strategies

  • **Inbox Pattern:** Save the ID of every processed message in a table. Before processing a new message, check if the ID already exists.
  • **Upsert Logic:** Use SQL `UPSERT` or `MERGE` commands so that if the record exists, the second call just updates it (or does nothing) instead of creating a duplicate.

4. Interview Mastery

Q: "How do you handle idempotency at the API level?"

Architect Answer: "We require an **Idempotency-Key** header in the request. We store the result of that key's processing in Redis for 24 hours. If we receive the same key again, we skip the business logic and just return the cached response. This protects our system from double-clicks and retry-logic from the client side."

Microservices & Event-Driven Architecture (EDA) Mastery
1. Foundations of Microservices
The Monolith to Microservices transition: When and why? Domain Driven Design (DDD): Bounded Contexts and Aggregates Database Per Service: Managing data consistency Service Discovery and Health Checks in .NET
2. Communication Patterns
Synchronous Communication: HTTP/gRPC and Service Mesh Asynchronous Communication: Message Brokers (RabbitMQ/Kafka) API Gateways: YARP (Yet Another Reverse Proxy) vs Ocelot Protobuf and Shared Contracts: Managing breaking changes
3. Event-Driven Architecture (EDA)
Introduction to EDA: Producers, Consumers, and Topics The Publisher/Subscriber Pattern in .NET Event Sourcing: Capturing every state change CQRS (Command Query Responsibility Segregation) with MediatR
4. Distributed Transactions & Resiliency
The Saga Pattern: Orchestration vs Choreography The Outbox Pattern: Ensuring reliable message delivery Idempotency: Preventing duplicate message processing Distributed Locking with Redis (Redlock)
5. Observability & Monitoring
Distributed Tracing with OpenTelemetry Centralized Logging: ELK Stack (Elasticsearch, Logstash, Kibana) Metrics and Dashboards: Prometheus and Grafana Correlation IDs: Tracking requests across services
6. Security & Identity
Centralized Authentication: IdentityServer4 & Duende Identity OAuth2 and OIDC Flow for Microservices API Key Management and Rate Limiting Mutual TLS (mTLS) for Internal Service-to-Service Security
7. Infrastructure & Deployment
Containerization: Production-grade Dockerfiles Kubernetes for .NET: Pods, Services, and Ingress Helm Charts: Managing complex deployments Blue-Green and Canary Deployments in K8s
8. FAANG Microservices Case Studies
Case Study: Designing a Global Notification Engine (Reliability at Scale) Case Study: Building a High-Performance Logging Pipeline (PB/Day)