Microservice Patterns
Service Discovery
Instances come and go with every deploy, crash and scale event. Service discovery keeps callers pointed at the instances that exist right now — from the client side via a registry, or from the server side via a load balancer.
What you will learn
- Explain why hard-coded addresses break in dynamic environments.
- Compare client-side and server-side discovery.
- Understand registration (self vs third-party) and staleness.
- Recognise what Kubernetes and service meshes already do for you.
Before you start
Overview
A taxi company does not give customers a driver's phone number — drivers change shifts all day. Customers call the dispatcher, who knows which drivers are on duty right now.
In the cloud, instances get new addresses whenever they are deployed, rescheduled or scaled. A service registry keeps the current list: instances register on start-up (or a platform registers them), are removed when health checks fail, and callers find them through client-side discovery (ask the registry, load balance yourself) or server-side discovery (call one stable address; a router asks the registry).
Run it
At t = 12 s a rolling update replaces orders-2 with orders-4. Watch the failures and Served by the new orders-4 in each design, and try the cache refresh slider in B.
Client-side vs server-side
| Client-side (Eureka, Consul + client) | Server-side (Kubernetes Service, cloud LB, mesh) | |
|---|---|---|
| Who picks the instance | The caller's library | A router / proxy |
| Extra hop | No | Yes (often on the same node) |
| Language support | Needs a client per language | Any HTTP/gRPC client |
| Staleness | Up to the cache refresh interval | Up to the health-check interval |
| Smart routing | Easy (zones, versions) in the client | In the proxy / mesh config |
Examples
apiVersion: v1
kind: Service
metadata: { name: orders }
spec:
selector: { app: orders } # every ready pod with this label is an endpoint
ports: [{ port: 80, targetPort: 8080 }]
# Callers use http://orders — DNS + kube-proxy route to ready pods; readiness probes remove bad ones.@Bean @LoadBalanced
RestClient.Builder restClientBuilder() { return RestClient.builder(); }
// "orders" is a service id resolved from the registry (Eureka/Consul), not a host name.
Order order = restClientBuilder.build().get().uri("http://orders/api/orders/{id}", id).retrieve().body(Order.class);Trade-offs
| Benefits | Costs | |
|---|---|---|
| Client-side | No extra hop; rich client-side routing. | Discovery logic in every service and language; caches go stale. |
| Server-side | Callers stay simple; one place to configure routing. | An extra hop and a component that must be highly available. |
| Registry | Single source of truth for what is running. | Critical dependency: if it is down or wrong, nothing can be found. |
Production considerations
- On Kubernetes, prefer the built-in Service/DNS discovery unless you need client-side features.
- Keep DNS TTLs and client caches short, and make clients retry on another instance after a connection error.
- Use readiness (not liveness) to decide registration, so warming instances get no traffic.
- Run the registry replicated; let clients keep their last known list if it is briefly unavailable.