SteelReed

Search

Search posts, questions…

7. System Design

Service discovery, central config and gateways: platform or library (Spring Cloud)

Why you can't hard-code host:port and per-instance settings; the four jobs every multi-service system must solve (discovery, shared config, one front door, fault tolerance) and whether Kubernetes/a mesh or an in-app library should do them. Spring Cloud (Config, Eureka, LoadBalancer, Gateway, CircuitBreaker) with a real lab; how to review AI-written config.

19 Nov 20267 min readSenior

–

1. The problem

payment-service calls 6 order-service instances from a hard-coded host:port list. Autoscaling adds 2 instances and they get no requests. A dead instance still gets 1/6 of the calls.

2. Key idea

Addresses and config change all the time, so you need one shared source of truth. That source is always a few to tens of seconds behind: a crashed instance can't announce it, it only disappears after a check cycle.

Job Platform does it Library does it
Service discovery (name → live instances) Kubernetes Service + DNS, Consul DNS Eureka
Central config ConfigMap, Secret Config Server
Gateway (entry point: auth, rate limits, routing) Ingress, Gateway API, Envoy Spring Cloud Gateway
Fault tolerance service mesh (Istio, Linkerd) Resilience4j

2.1 Platform or library?

Choice Use when Don't use when
Platform (K8s Service, ConfigMap) already on Kubernetes, many languages on VMs with nobody to run it
Library (Eureka, Config Server) VMs or bare metal, all Java already on Kubernetes: two discovery systems
Spring Cloud Gateway routes and filters with Java logic only TLS and path routing: Ingress is enough
In-app circuit breaker you need business fallbacks the mesh already breaks circuits and no fallback is needed

2.2 Why does a dead instance still get requests?

  • Eureka only learns an instance died from missing heartbeats: the lease expires after 90 seconds, eviction runs every 60.
  • Losing 1 of 2 instances drops heartbeats to 50%, below the 85% threshold: self-preservation kicks in and Eureka stops evicting.
  • The gateway caches the registry for 30 seconds, and LoadBalancer caches for another 35.

Rules: one system per job; discovery doesn't replace timeouts and retries; changing config at runtime is a production change, so review it and canary it.

3. In Java

PROPERTIES
# order-service: fetch config at startup, register with Eureka
spring.application.name=order-service
spring.config.import=optional:configserver:http://config:8888
eureka.client.service-url.defaultZone=http://localhost:18761/eureka
 
# gateway: route by service name, retry when an instance dies
spring.cloud.gateway.server.webflux.routes[0].id=orders
spring.cloud.gateway.server.webflux.routes[0].uri=lb://order-service
spring.cloud.gateway.server.webflux.routes[0].predicates[0]=Path=/orders/**
spring.cloud.gateway.server.webflux.routes[0].filters[0]=Retry=2
spring.cloud.gateway.server.webflux.httpclient.connect-timeout=500
spring.cloud.gateway.server.webflux.httpclient.response-timeout=2s
# kill -9 one instance: ok=40 fail=0

Calling another service from code: the RestClient.Builder needs @LoadBalanced; without it http://order-service goes to DNS and throws UnknownHostException. Other languages: Go and Node.js usually rely on Kubernetes DNS or a Consul client.

4. Working with AI

Give the AI: gateway route skeletons, migrating old prefixes to new ones, a script that calls in a loop to count errors when one instance is kill -9ed.

Check in AI-written code:

  • The Spring Cloud version matches the Boot line (2025.1 for Boot 4.0/4.1).
  • The prefix is spring.cloud.gateway.server.webflux.* or .webmvc.*, not spring.cloud.gateway.routes.
  • Retry covers only GET or endpoints with an idempotency key, and only at one layer.
  • The gateway has connect and response timeouts.
  • WebFlux gateway filters don't call JDBC or block().

5. Exercises

Exercise 1: Review a gateway config

The AI wrote this, described as "resilient: retries every request 3 times, checks quota in a filter":

PROPERTIES
spring.cloud.gateway.routes[0].uri=lb://payment-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/payments/**
spring.cloud.gateway.routes[0].filters[0].name=Retry
spring.cloud.gateway.routes[0].filters[0].args.retries=3
spring.cloud.gateway.routes[0].filters[0].args.methods=GET,POST
spring.cloud.gateway.routes[0].filters[1]=QuotaFilter   # reads quota with JdbcTemplate

Find the bugs and fix them.

Solution to Exercise 1
  • Retrying POST /api/payments: the request may have charged the card before the connection dropped; resending charges twice. Retry only GET, or require an idempotency key.
  • QuotaFilter calls JDBC on the event loop: a few slow requests take every reactor-http-nio-* thread and all routes stall. Use RequestRateLimiter with reactive Redis, or switch to Gateway Server MVC.
  • Old prefix: since 2025.0, use spring.cloud.gateway.server.webflux.routes.

Exercise 2: A dead instance still gets requests

Eureka, two order-service instances, a gateway route lb://order-service, no Retry yet. kill -9 one instance and call once a second for 200 seconds: ok=96 fail=98, and the Eureka UI says EMERGENCY. Diagnose, and fix it so users see no errors without waiting for Eureka.

Solution to Exercise 2

Diagnosis per section 2.2: death is only noticed by missing heartbeats, self-preservation blocks eviction, and two cache layers add delay.

Fix on the caller: Retry=2 for GET plus a short connect timeout (500 ms). Each attempt goes back through the load balancer and round robin picks the live instance: the rerun gave ok=40 fail=0 even with the dead instance still registered. On Kubernetes a failing readiness probe removes the pod at once, and Eureka can go.

Summary

  • Four jobs: discovery, shared config, gateway, fault tolerance; each lives in the platform or a library.
  • The registry is always behind: callers need timeouts and retries.
  • Retry only idempotent requests, and only at one layer.
  • Missing @LoadBalanced means UnknownHostException.
  • On Kubernetes you usually drop Eureka; never run two discovery systems.

Read more: Timeouts, retries and circuit breakers.


Self-compiled notes from official documentation and hands-on practice.

Practice interview questions on this topic

All Microservices questions →All System Design questions →

Loading comments…
Next on the roadmapDistributed transactions: 2PC, TCC, saga, pivot transactions and the transactional outbox