API Gateway Pattern
How to
explain in interview
“API
Gateway is a single-entry point for all client requests. It handles routing,
authentication, rate-limiting, and API aggregation before forwarding to backend
microservices.”
Problems
it solves
- Removes
client-to-multiple-service complexity
- Centralized
auth
- Prevents
exposing internal microservices
- Supports
canary release & throttling
Use
case
Mobile app
+ web app → need lightweight, optimized API responses.
Java
tools
- Spring
Cloud Gateway
- Netflix
Zuul
- Kong
/ Nginx
Follow-up
question
If API
Gateway goes down, whole system fails — how do you avoid?
✔ Answer:
Run gateway in active-active mode behind a load balancer, with autoscaling
& distributed config.
Backend For Frontend (BFF)
Interview
explanation
“BFF
creates separate gateways for mobile, web, and partner applications so that
each frontend gets a tailored response.”
When
needed
- Mobile
app needs fewer fields
- Web
UI needs detailed data
Tools
- Spring
Boot BFFs
- GraphQL
BFF (Netflix uses)
Follow-up
❓
Difference between API Gateway and BFF?
✔ Answer:
BFF is client-specific; Gateway is system-wide.
Saga Pattern – (MOST IMPORTANT for
architect interviews)
Interview
explanation
“Saga
handles distributed transactions across microservices using either choreography
(events) or orchestration (central controller). It replaces 2PC in
microservices.”
Why
needed
- DB
per service means no cross-service transactions
- Ensures
consistency using compensating actions
Example
Order →
Payment → Inventory
If inventory fails → compensate Payment → compensate Order.
Implementations
- Choreography:
Kafka events
- Orchestration:
Camunda / Temporal / Axon
Follow-up
❓
When to use choreography?
✔
Lightweight, fewer services, event-driven.
❓
When to use orchestration?
✔
Complex workflows, error handling, timeouts.
CQRS (Command Query Responsibility
Segregation)
Interview
explanation
“CQRS
separates write (commands) and read (queries) models to improve performance,
scalability, and optimizes read-heavy systems.”
Benefits
- Read
DB can be NoSQL & denormalized
- Commands
enforce validations
- Event
sourcing integrates naturally
Use
case
- Banking
- Wallet
applications
- Order
tracking dashboards
Follow-up
❓
Downsides of CQRS?
✔
More complexity, eventual consistency, duplicate databases.
Event Sourcing
Interview
explanation
“Instead
of saving final state, we store all events. System state can be rebuilt by
replaying events.”
Benefits
- Perfect
audit history
- Rollback
to past states
- High
write performance
Use
case
- Ledger
systems
- Audit-driven
domains
- Payment
& transactions
Java
tools
Follow-up
❓
How do you prevent event store from becoming too large?
✔
Snapshotting after fixed number of events.
Strangler Fig Pattern (Migration
Pattern)
Interview
explanation
“We
gradually replace a monolith by routing specific functionality to a new
microservice until the monolith is fully replaced.”
When
needed
- Legacy
→ Microservices migration
- Zero
downtime rollout
Example
Move inventory
functionality from monolith → microservice → redirect traffic via gateway.
Follow-up
❓
How do you ensure backward compatibility?
✔
Versioned APIs and backward-compatible schema changes.
Database per Service Pattern
Interview
explanation
“Each
microservice owns its database. This decouples services, enables independent
scaling, and avoids distributed locking.”
Challenges
solved
- No
shared schema
- No
runtime coupling
- Independent
release cycles
Hard
question
❓
How do you maintain data integrity without foreign keys?
✔
Using Saga, events, or domain-driven consistency guarantees.
Outbox Pattern
Architect-level
explanation
“Outbox
ensures reliable event publication using a single local transaction: write to
DB + write event into outbox table.”
Then a
background job publishes events to Kafka.
Why
needed
- Prevents
lost events during crashes
- Ensures
atomicity without 2PC
Follow-up
❓
How do you implement Outbox efficiently?
✔
With Debezium CDC + Kafka; avoids custom polling.
Circuit Breaker Pattern
Interview
explanation
“Circuit
breaker stops calling failing services and gives fallback immediately. This
avoids cascading failures.”
Java
tools
- Resilience4j
(recommended)
- Hystrix
(deprecated but still asked)
State
transitions
- Closed → Open → Half-Open →
Closed
Follow-up
❓
Difference between retry & circuit breaker?
✔
Retry handles temporary failures; circuit breaker handles persistent failures.
Bulkhead Pattern
Interview
explanation
“Bulkhead
isolates resources (thread pools, connections) so failure in one service
doesn’t affect others.”
Example
If Payment
API is slow → only its pool is consumed → Order API still runs.
1️Retry + Timeout Pattern
Interview
explanation
“Retries
must be combined with timeouts & backoff to avoid retry storms.”
Java
- Resilience4j
Retry
- Spring
Retry
Follow-up
❓
How many retries?
✔
Based on SLA, usually 2–3 with backoff.
Sidecar + Service Mesh Pattern
Interview
explanation
“Service
mesh offloads observability, security, and traffic control to sidecars instead
of writing logic inside services.”
Features
- mTLS
- Retries
- Circuit
breaking
- Canary
& blue-green rollout
Tools
Follow-up
❓
How is service mesh different from API gateway?
✔
Gateway is north-south traffic; mesh is east-west traffic.
Centralized Configuration Pattern
Interview
explanation
“Configuration
is stored centrally and fetched at runtime.”
Java
tools
- Spring
Cloud Config
- Consul
- AWS
Parameter Store
Benefits
- Secure
secret handling
- No
restart needed
- Versioned
config
Distributed Logging & Tracing
Pattern
Interview
explanation
“Microservices
generate logs in multiple nodes, so we need centralized log aggregation and
distributed tracing.”
Tools
- ELK
- OpenTelemetry
- Zipkin
- Jaeger
Follow-up
❓
How do you trace one request across 20 microservices?
✔
Pass correlation ID through all services (propagated via headers).
Aggregator Pattern
Interview
explanation
“Aggregator
composes data from multiple microservices into a single response.”
Use
case
- Dashboard
API
- Combining
Order + User + Inventory data
Anti-Corruption Layer (ACL)
Interview
explanation
“ACL
isolates microservices from legacy systems using translation adapters.”
Use
case
Legacy
SOAP → ACL → New REST Microservice.
Service Registry and Discovery:
- Pattern: Service Discovery
- Description: A service registry is used to keep track of the available microservices and their locations. Services register themselves with the registry, and clients can discover and communicate with services through the registry.
- Benefits: Enables dynamic scaling and seamless service discovery.
Event Sourcing:
- Pattern: Event Sourcing
- Description: Instead of storing only the current state of data, all changes to the state are captured as a sequence of events. The system's state can be reconstructed at any point by replaying the events.
- Benefits: Provides a reliable audit trail, supports scalability, and allows for building event-driven architectures.
Bulkhead Pattern:
- Pattern: Bulkhead Pattern
- Description: Segregates components into isolated pools to prevent the failure of one component from affecting others. For example, thread pools can be used to isolate requests to a specific microservice.
- Benefits: Enhances system resilience by isolating failures and limiting their impact.
Choreography vs. Orchestration:
- Pattern: Choreography and Orchestration
- Description: Defines how microservices collaborate to achieve a specific goal. Choreography is a decentralized approach where each service communicates directly with others. Orchestration is a centralized approach where a central component (orchestrator) coordinates the interactions.
- Benefits: Flexibility with choreography and centralized control with orchestration.
API Composition:
- Pattern: API Composition
- Description: Instead of relying on a single microservice to fetch and aggregate data from multiple sources, each microservice retrieves and composes its own data through API calls. This pattern helps maintain independence and scalability.
- Benefits: Reduces dependencies between microservices, allowing them to evolve independently.
API
Composition Patterns
Aggregator
Pattern
One
component calls multiple microservices and aggregates data.
Ideal
for:
- Dashboard
APIs
- Mobile
apps
Strangler
Fig Pattern
Used
for migrating monolith → microservices.
Gradually
replace old modules with new microservices.
Anti-Corruption
Layer
Protect
microservices from legacy systems.
Example:
- Converting
old SOAP response → REST JSON
- Data
format mapping