Microservices Architecture

Independently deployable services organized around business capabilities, each owning its data. Benefits, real costs, sizing, communication and the prerequisites teams often skip.

Intent

Structure an application as a set of small, autonomous services that are independently deployable, organized around business capabilities, owned by a single team, and communicating over the network. Each service owns its data.

flowchart LR
  GW[API gateway] --> O[Order service]
  GW --> C[Catalog service]
  GW --> U[Customer service]
  O --> ODB[(Orders DB)]
  C --> CDB[(Catalog DB)]
  U --> UDB[(Customer DB)]
  O -- events --> K[[Event broker]]
  K --> I[Inventory service] --> IDB[(Inventory DB)]
  K --> N[Notification service]

Prerequisites (often skipped)

  • Automated CI/CD per service and infrastructure as code.
  • Observability: distributed tracing, centralized logs, metrics.
  • Teams aligned to services (Conway's law); clear ownership.
  • Platform capabilities: service discovery, config, secrets, resilience defaults.
  • Contract testing and API versioning practices.

Sizing guidance

Size by bounded context and team ownership, not lines of code. A service should be small enough for one team to own and change independently, and large enough that most changes do not span several services. If every feature touches five services, boundaries are wrong.

Real-world example

Netflix and Amazon popularized the style to let hundreds of teams deploy independently. Amazon's two-pizza teams own services end to end ("you build it, you run it").

Pros and cons

Pros Cons
Independent deployment and scaling Network latency and partial failures
Team autonomy and parallel delivery Distributed data consistency (sagas, eventual consistency)
Technology choice per service Operational overhead: many pipelines, runtimes, dashboards
Fault isolation (if designed well) Harder debugging and testing across services
Smaller codebases per team Risk of distributed monolith

Java example: synchronous call with resilience

@Configuration
class Clients {
    @Bean
    RestClient inventoryClient(RestClient.Builder builder) {
        return builder.baseUrl("http://inventory-service")
                      .requestFactory(new JdkClientHttpRequestFactory())   // works well with virtual threads
                      .build();
    }
}

@Service
class AvailabilityService {
    private final RestClient inventory;
    AvailabilityService(RestClient inventoryClient) { this.inventory = inventoryClient; }

    @CircuitBreaker(name = "inventory", fallbackMethod = "unknown")
    public Availability check(String sku) {
        return inventory.get().uri("/stock/{sku}", sku).retrieve().body(Availability.class);
    }
    Availability unknown(String sku, Throwable t) { return Availability.unknown(sku); }
}

When not to use

Early-stage products, small teams, low operational maturity, or domains where almost every operation needs strong consistency across entities.

Interview questions

What is a distributed monolith and how do you avoid it?

Services that must be deployed together, share databases, or call each other synchronously in long chains. Avoid it with bounded-context boundaries, data ownership, asynchronous integration where possible, backward-compatible APIs and consumer-driven contract tests.

How do microservices communicate?

Synchronously (REST/gRPC) when the caller needs an immediate answer, asynchronously (events/messages) for decoupled reactions and workflows. Prefer async across bounded contexts; keep sync chains short with timeouts and circuit breakers.

How do you handle reporting across many service databases?

Do not query service databases directly. Stream changes (CDC/events) into a warehouse or lakehouse and build reporting models there.