Skip to content
Rotha Dapravith

Retail microservices platform

A Spring Cloud Gateway in front of product and invoice services, with Keycloak-issued JWTs, per-route circuit breakers and Kafka for asynchronous product creation.

Type
Personal project
Year
2025
Status
Runs locally
Stack
  • Java
  • Spring Cloud Gateway
  • Keycloak
  • Kafka
  • Resilience4j
  • OpenFeign
  • PostgreSQL
  • Flyway
  • Docker

The problem

A personal project for practising how a small retail back office splits into services: a product catalogue, invoicing, and one gateway that every client request passes through. It runs locally with Docker; there is no public deployment.

Give clients a single authenticated entry point while keeping the services independent, stop one slow or failing service from tying up the gateway, and accept product-creation requests quickly even when the product service is busy.

My role

Personal project. I wrote the gateway, the product and invoice services, their database migrations and the local Docker and Kafka setup.

Approach

The design puts each concern in one place. Identity is checked once, at the edge. Failure isolation is a gateway rule per route, not code in every caller. Work that can wait goes onto a Kafka topic instead of a synchronous call. Each service owns its schema through its own Flyway migrations, so services can change independently.

Architecture

Clients only talk to the gateway. It validates the Keycloak token, rewrites the path and forwards the request to the owning service through a circuit breaker. Writes that can be processed later go onto a Kafka topic instead of a synchronous call.

  1. Client
    • API clientSwagger UI, Postman or a frontend
  2. Edge
    • Spring Cloud GatewayJWT check · circuit breakers · trace IDs
    • Keycloakissues tokens for the retail realm
  3. Services
    • product-servicecatalogue · Kafka consumer
    • invoice-serviceinvoices · Feign client to products
  4. Data & messaging
    • PostgreSQLone Flyway-managed schema per service
    • Kafkatopic new-products · 3 local brokers
System overview. Tiers read top to bottom; connections are listed below.

Request flow

  1. The client gets an access token from Keycloak and calls the gateway with it.
  2. The gateway verifies the token against Keycloak’s published keys, then routes /product-service/** and /invoice-service/** to the matching service.
  3. Each route is wrapped in a Resilience4j circuit breaker; while it is open, requests go to a fallback endpoint instead of waiting.
  4. POST /v1/async/product publishes to the new-products topic and returns 202 Accepted; product-service consumes the event and saves it in a transaction.
  5. invoice-service reads product data through an OpenFeign client.

Technical decisions

Spring Cloud Gateway
Routing, authentication and resilience rules live in gateway configuration instead of being repeated in each service.
Keycloak
A standard OpenID Connect provider, so services trust signed tokens rather than managing users.
Resilience4j
One circuit breaker per route (opens at a 50% failure rate over a 10-call window), so a failing service fails fast.
Kafka
Decouples product creation from the request that asks for it.
Flyway
Versioned SQL migrations, so each service owns and evolves its own schema.
OpenFeign
A declarative client for the one synchronous call between services.

Implementation

The gateway is configured almost entirely in YAML: each route has a path rewrite and a named circuit breaker. A global filter takes the current trace ID from Micrometer Tracing, adds it to the logging context and returns it in an X-Trace-ID response header, so an error a client reports can be matched to server logs.

Security is a resource-server setup. Every path except /actuator/** needs a valid JWT, and authentication failures go through a custom entry point that returns a consistent JSON error body.

Each service has its own Dockerfile and Spring profiles for local and container runs. One Compose file starts a three-broker Kafka cluster with ZooKeeper; another starts Keycloak with Prometheus.

Challenges and solutions

  1. Challenge

    A slow or failing service can hold gateway requests open and spread the failure to every client.

    Solution

    A Resilience4j circuit breaker per route opens at a 50% failure rate over a 10-call window and sends requests to a fallback endpoint instead of waiting.

  2. Challenge

    An error a client reports is hard to match to the right lines in several services’ logs.

    Solution

    A global gateway filter puts the Micrometer trace ID into the logging context and returns it in an X-Trace-ID response header.

  3. Challenge

    Product creation should not depend on the product service being responsive at the moment of the request.

    Solution

    POST /v1/async/product publishes to the new-products topic and returns 202 Accepted; product-service consumes the event and saves it in a transaction.

  4. Challenge

    Clients need a predictable error when a token is missing or invalid.

    Solution

    A custom authentication entry point returns the same JSON error body for every authentication failure.

Security and reliability

  • Tokens are verified against Keycloak’s signing keys; services never handle passwords.
  • Only the health and prometheus actuator endpoints are exposed, for health checks and metrics scraping.
  • Circuit breakers and a fixed-backoff retry template (three attempts, two seconds apart) limit the impact of a slow dependency.

Testing

Each service has a Spring Boot context-load test, which catches wiring and configuration errors but not behaviour. API flows are checked by hand through Swagger UI and Postman.

Trade-offs

  1. Choice

    Asynchronous product creation through Kafka

    What it costs

    The client gets 202 before the event is processed and, without a status endpoint, cannot learn whether creation succeeded.

  2. Choice

    A single gateway as the only entry point

    What it costs

    Every request takes an extra hop, and the gateway itself becomes a component that has to stay available.

  3. Choice

    A three-broker Kafka cluster for local runs

    What it costs

    Closer to a real cluster’s behaviour, but heavier to start on a laptop than a single broker.

Results

One runnable repository that shows token validation at the edge, per-route circuit breakers, trace IDs returned to clients and an event-driven write path.

Lessons and next steps

Known limitations

  • The gateway has a route to a user-service that is not part of this repository.
  • There are no behavioural tests yet, and the CI workflow the README describes is not committed.
  • The asynchronous endpoint answers 202 before the event is processed and offers no status endpoint, so a client cannot find out whether creation succeeded.
  • Local configuration files are committed; a real deployment would load credentials from a secret store.

What I would change next

  • Add a status endpoint for asynchronous writes so a client can confirm the outcome.
  • Write behavioural tests for routing, fallbacks and the token check, and commit the CI workflow the README describes.
  • Load credentials from the environment or a secret store instead of committed local configuration.

Next projectFine-grained authorization with Keycloak