Service Discovery & Service Mesh
You'll learn to
- -Explain why hard-coded addresses fail when service instances come and go
- -Trace registration, discovery, heartbeats and removal of a dead instance through a service registry
- -Compare client-side and server-side discovery
- -Explain what a service mesh adds with sidecar proxies, and what it costs
Once services are split apart and each can scale, deploy and restart independently, a new problem appears. How does the Orders service know the current network address of the Users service, when that address can change every time an instance restarts, crashes, or is added during a traffic spike? A hard-coded address works on the day you write it and goes stale the day after.
An Analogy: The Company Directory
In a large company, people move desks and change extensions all the time. Nobody memorizes phone numbers. Instead there is a company directory, and when you need someone you look them up and call the extension it shows. The directory only works if it is kept current: people register when they arrive, and someone who has left is removed. A good directory also notices when a person has stopped answering their desk phone.
- -The people are service instances, which come and go.
- -The directory is the service registry.
- -Registering on arrival is registration, and someone proving they are still at their desk is a heartbeat.
- -Looking someone up before calling is service discovery.
Service Discovery
Each service instance registers itself with a shared registry when it starts, and removes itself, or is detected as gone, when it stops. Callers ask the registry for the current addresses instead of using fixed IP addresses. The card below describes the registry component as it appears in the lab:
In a microservice world, services are born, die, and move constantly. When your Order Service needs to call the Inventory Service, how does it know WHERE the Inventory Service is running? The IP address changes every time it restarts, and there might be 5 instances running at different addresses. A Service Registry is the live directory of all running services. Every service, when it starts up, registers itself: 'Hi, I'm Inventory Service, running at 10.0.3.42:8080, and I'm healthy.' The registry regularly health-checks all registered services. When Order Service needs to talk to Inventory Service, it asks the registry: 'Where is Inventory Service?' and gets back a list of healthy instances. This is SERVICE DISCOVERY. The ability to find services dynamically without hardcoding IP addresses. It's like the reception desk in a large hospital: instead of memorizing which room every doctor is in (rooms change all the time), you ask reception and they look up the current location.
Examples: Consul (HashiCorp, built-in health checks and key/value store), Eureka (Netflix, for Spring Boot services), AWS Cloud Map, Kubernetes Service + kube-dns (built into K8s), etcd (can be used for discovery)
Tracing One Lookup
- -A Users instance starts at 10.0.3.17:8080 and registers: the name users maps to that address. It then sends a heartbeat every 10 seconds.
- -The Orders service asks the registry where users is, and receives two addresses, 10.0.3.17 and 10.0.3.22. It picks one in turn and caches the list for a short time, say 30 seconds, so it does not ask on every call.
- -The instance at 10.0.3.17 crashes. Its heartbeats stop, and after a timeout the registry drops it.
- -Until callers refresh their cache, a few calls may go to the dead address and fail. A well-behaved client retries on another instance, and soon everyone sees only the surviving one.
The diagram shows the pattern: every instance registers, and the caller asks the registry for a current address.
Every instance registers itself on startup; the Orders service asks the registry for a current address instead of using a hardcoded one.
Consul, etcd and ZooKeeper are common registries. In Kubernetes, discovery is built in: a service's name resolves to whichever of its instances are currently healthy.
Client-Side or Server-Side?
- -Client-side discovery: the caller queries the registry and chooses an instance itself. There is one hop fewer, but every client needs the discovery and load-balancing logic.
- -Server-side discovery: the caller sends its request to a load balancer or router, which consults the registry. Clients stay simple, but there is another component to run and keep available.
Service Mesh: Discovery, Plus Everything Around the Call
As the number of services grows, every one of them needs the same things around its calls: find the other service, retry on failure, time out, encrypt the traffic, and report metrics. Putting that logic into a library in each service means writing it once per language and updating every service to change it. A service mesh moves it out of the code. Next to every service instance runs a small proxy, a sidecar (Envoy is the common one), and all traffic in and out goes through it. A control plane configures every sidecar from one place. It is as if every employee had an assistant who makes their calls: placing the call, retrying if it does not connect, checking the other person's identity and keeping a log.
- -Security: mutual TLS between all services, so each proves who it is and the traffic is encrypted, with no application changes.
- -Reliability: retries, timeouts and circuit breaking configured centrally.
- -Traffic control: send 5% of requests to a new version to test it, or shift traffic gradually.
- -Visibility: metrics and traces for every call, for free.
The costs are real: each call now passes through two extra proxies, which adds a little latency, every instance uses extra memory and CPU for its sidecar, and the mesh itself is a complex system to operate. It earns its place with dozens of services, strict security requirements, or many languages. With three services it is overkill. The Wiki card below goes further:
An infrastructure layer that handles service-to-service communication concerns (mTLS, circuit breaking, retries, observability) transparently, without changes to application code.
Imagine every employee in a company personally negotiated security protocols, handled phone failures, tracked who they called, and logged every conversation manually. A service mesh is the company's centralized communication department - it handles all of that so employees just make the call without worrying about the mechanics.
A service mesh is built around the sidecar pattern: a lightweight proxy (e.g., Envoy) is deployed alongside every service container in the same pod. All network traffic flows through these sidecars, which implement: mutual TLS (mTLS), load balancing, circuit breaking, retries, timeouts, distributed tracing, and metrics - without any application code changes.
Mutual TLS (mTLS): Every service-to-service call is encrypted AND both sides authenticate with certificates. This eliminates the need for API keys or tokens for internal service calls. Istio auto-issues and rotates certificates.
Traffic management: Fine-grained control - send 10% of traffic to the new version (canary), send all traffic from users with header debug: true to the staging cluster, automatically retry failed requests with exponential backoff.
Observability: The mesh captures metrics (latency, error rate, throughput) and distributed traces for EVERY service call across the entire system - without any instrumentation in application code.
The cost: significant operational complexity. Running Istio requires understanding CRDs (Custom Resource Definitions), Envoy configuration, certificate rotation, and control plane management. Many teams add a service mesh prematurely as complexity for complexity's sake.
- -Only justified at scale: 10+ microservices with complex inter-service communication.
- -Envoy proxy is the universal data plane (used by Istio, Linkerd, Consul Connect).
- -mTLS in a service mesh is essentially free zero-trust networking.
- -Linkerd is simpler than Istio; use Linkerd for teams without a dedicated platform team.
- -Cilium (eBPF-based) is the emerging alternative - no sidecar overhead.
In one line: addresses change constantly, so services register themselves in a registry and callers look them up, with heartbeats to drop dead instances. A service mesh goes further, moving retries, encryption and metrics into a sidecar next to every service, at the price of complexity.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!