Choosing the Right Pattern

Problem to Pattern Cheat Sheet

ProblemPatternWhere covered
Clients should not know every service addressAPI GatewayMedium level
Services find each other dynamicallyService discovery (Eureka / Kubernetes DNS)Medium level
A slow dependency takes everything downCircuit breaker, timeouts, bulkheadMedium level
Business transaction across servicesSaga (choreography / orchestration)This level
Database change + event must both happenTransactional outboxThis level
Duplicate messages and retriesIdempotent consumer, idempotency keyThis level
Screen needs data from many servicesAPI composition, BFFThis module
Complex or heavy queries across servicesCQRS read modelThis module
Migrating a legacy monolithStrangler fig + anti-corruption layerThis module
Cross-cutting concerns (TLS, retries) in every serviceSidecar / service meshSecurity module

Common Anti-Patterns

Anti-patternSymptomFix
Distributed monolithEvery release needs 5 services deployed togetherRedraw boundaries around business capabilities; async events
Shared databasepayment-service reads order tables directlyDatabase per service, data via API or events
Chatty servicesOne page = 40 synchronous callsBatch endpoints, composition, CQRS read model
Nano-servicesA service per table or per functionMerge into one service per bounded context
No observability"It works on my machine" across 15 servicesTracing, metrics, structured logs from day one

Tip: Patterns have costs. Start with the simplest thing (a modular monolith, synchronous calls) and add a pattern when a real problem appears. Interviewers like candidates who can say when not to use a pattern.

Exercise

Exercise: ShopEase wants an admin page: "top 10 products by revenue in the last 7 days, with category name and current stock". Data is in order-service (order items), product-service (category) and inventory-service (stock). Which pattern would you use and why?

Show answer
Use a CQRS read model (an analytics/reporting projection), not live API composition.

- Composition would need ALL order items of 7 days from order-service, then
  product and stock for each product: slow, heavy, and repeated on every page load.
- Instead, a "product-sales-view" service listens to OrderPlaced / OrderCancelled,
  ProductUpdated and StockChanged events and keeps a table:
      product_id | name | category | revenue_7d | stock
- The admin page runs one indexed query:  ORDER BY revenue_7d DESC LIMIT 10.
- Trade-off: data is a few seconds behind, which is fine for a dashboard.