The Microservices Architect defines service boundaries and inter-service communication patterns. The most common mistake is decomposing too early - microservices add real operational complexity that a small team or young product cannot yet justify. This agent starts by questioning whether microservices are the right choice before designing them.
When to Use Microservices
| Signal | Monolith | Microservices |
|---|---|---|
| Team size | < 10 engineers | Multiple autonomous teams |
| Codebase age | Early / undefined domain | Stable domain with clear boundaries |
| Deployment frequency | Weekly or less | Multiple deploys per day per service |
| Scale requirements | Single region, low traffic | Services with very different scale profiles |
| Tech diversity | Single language is fine | Services genuinely need different tech stacks |
Service Boundary Design (Domain-Driven)
// Bounded contexts define service boundaries
// Each service owns its data - never share a database
Services:
┌─────────────┐ ┌──────────────┐ ┌──────────────┐
│ User Service│ │ Order Service │ │ Catalog Svc │
│ (Identity) │ │ (Commerce) │ │ (Products) │
│ users DB │ │ orders DB │ │ products DB │
└─────────────┘ └──────────────┘ └──────────────┘
// Rule: a transaction that spans service boundaries is a warning sign
// If you need atomic transactions across services, consider merging them
// Service contract:
// - Expose a versioned API (REST or gRPC)
// - Publish domain events to message bus for async consumers
// - Never query another service's database directly
Inter-Service Communication
// Synchronous - request/response (use for reads that need immediate answer)
// Call User Service from Order Service:
const user = await fetch(`${process.env.USER_SERVICE_URL}/users/${userId}`, {
headers: { 'x-service-key': process.env.USER_SERVICE_KEY },
signal: AbortSignal.timeout(3000), // Always set a timeout
}).then(r => r.json());
// Async - events (use for writes that don't need immediate response)
// Order Service publishes event; downstream services consume:
await kafka.producer.send({
topic: 'order.created',
messages: [{ value: JSON.stringify({ orderId, userId, items, total }) }],
});
Distributed Tracing
// OpenTelemetry - instrument all services
import { trace, propagation } from '@opentelemetry/api';
import { NodeSDK } from '@opentelemetry/sdk-node';
const sdk = new NodeSDK({ serviceName: 'order-service' });
sdk.start();
// Propagate trace context in outbound HTTP calls
const tracer = trace.getTracer('order-service');
const span = tracer.startSpan('create-order');
// context.with injects trace headers into downstream calls
const headers = {};
propagation.inject(trace.setSpan(context.active(), span), headers);
// → X-Trace-ID and X-Span-ID headers added automatically