codez.guru

What Is Service Discovery?

In a microservice system, services often come and go dynamically (due to auto-scaling, deployments, or failures). So…
how does Service A know how to reach Service B?

Service discovery solves this by allowing services to register themselves and query other services dynamically.


Static vs Dynamic Discovery

πŸ”Ή Static Discovery

  • IPs or URLs are hardcoded
  • Simple, but fragile and not scalable

πŸ”Ή Dynamic Discovery

  • Services register themselves on startup
  • Others query the registry to get healthy instances

Example:

Service A β†’ DNS / registry β†’ find Service B's IPs β†’ connect

Using a Service Registry

A service registry keeps track of live service instances and their metadata (e.g., version, zone, port).

Popular tools:

  • πŸ”Έ Consul (by HashiCorp)
  • πŸ”Έ Eureka (by Netflix)
  • πŸ”Έ Etcd (used by Kubernetes)
  • πŸ”Έ Zookeeper (legacy systems)

Services register and deregister automatically.


What Is an API Gateway?

An API Gateway sits in front of your services and:

  • Routes traffic to the right service
  • Enforces authentication and rate limits
  • Performs logging, caching, and load balancing
  • Can transform requests/responses (e.g., GraphQL β†’ REST)

It’s your central access point.

Example:

Client β†’ API Gateway β†’ routes to UserService or OrderService

Popular Tools

Tool Type Description
Kong API Gateway Plugin-based, high performance
Envoy Proxy + Discovery Modern L7 proxy, built-in service discovery
NGINX Reverse Proxy Can serve as a simple gateway
Istio Service Mesh Adds policy, security, routing, telemetry
Traefik Gateway + Discovery Dynamic config with Docker/Kubernetes

Use gateways to centralize concerns β€” but avoid turning them into monolith bottlenecks.


Summary

Microservices rely on dynamic service discovery to stay flexible and resilient. API gateways help manage traffic, security, and monitoring across all services. Together, they keep your distributed system connected and reliable.


Next:
Lesson 08 – Service Mesh: Traffic Management, Observability, and Security