Service Registry and Service Discovery in Microservice
Service Registry and Service Discovery are complementary mechanisms that enable microservices to locate and communicate with each other dynamically, without hard-coded network addresses.
1. Service Registry
A Service Registry is simply a database of service instances (their network locations and metadata):
-
What it stores
-
Service name (e.g. “order-service”)
-
Instance ID (to differentiate multiple copies)
-
Hostname/IP and port
-
Optional metadata (version, region, health status)
-
-
How instances register
-
Self-registration: Each service, on startup, calls the registry’s API to add its own address.
-
Health checks: Registry periodically pings instances (HTTP/heartbeat) and removes unhealthy ones.
-
-
Examples
-
Eureka (Netflix OSS)
-
Consul (HashiCorp)
-
ZooKeeper (Apache)
-
2. Service Discovery
Service Discovery is the process by which a client or another service finds the network address of a target service:
2.1 Client-Side Discovery
-
Client queries the registry for instances of “order-service.”
-
It receives a list of healthy instances (e.g.
["10.0.1.12:8080", "10.0.1.13:8080"]
). -
Client performs client-side load balancing (round-robin, random) among them.
-
Pros: Full control of load-balancing logic in the client
-
Cons: Every client library needs registry integration
2.2 Server-Side Discovery (via API Gateway)
-
Client calls a single API Gateway endpoint (e.g.
api.myapp.com/orders
). -
Gateway queries the registry, picks a healthy instance, and forwards the request.
-
Response is proxied back to the client.
-
Pros: Simplifies clients, centralizes routing and security
-
Cons: Gateway can become a throttling bottleneck if not autoscaled
3. Typical Workflow
-
Startup
-
Service instance boots up and sends a “register” request to the registry.
-
-
Heartbeat
-
Registry pings each instance at regular intervals to confirm health.
-
-
Discovery Lookup
-
When Service A needs Service B, it queries the registry’s REST API or via a client-side library.
-
-
Load Balancing
-
The client or gateway selects one instance and makes the call.
-
-
Deregistration
-
On shutdown or failure, the instance is removed (gracefully or by timeout).
-
4. Why It Matters
-
Scalability: You can add or remove instances without reconfiguring clients.
-
Fault-Tolerance: Traffic is automatically routed around unhealthy instances.
-
Flexibility: Services can move between hosts or ports without breaking communication.
Mastering service registry and discovery is crucial for reliable, scalable microservices architectures—enabling services to find each other dynamically and stay resilient as your system grows.
Comments
Post a Comment