Service Discovery

When you have many microservices, one big question arises:

How do clients find a microservice and its available instances?

The Problem

Microservices often run in containers, and each container may get a dynamic IP address when it starts. This makes it tricky for clients to know where to send requests.

For example, imagine you have a User Service that exposes an HTTP API. Every time it restarts, it may get a new IP like 10.0.1.15 or 10.0.1.20. How will the client know the correct address?

Without a solution, requests may fail or go to the wrong instance.

The Solution

We introduce a Service Discovery Service — a special component that keeps track of all available microservices and their current addresses.

With service discovery:

img1

Implementation Approaches

There are two main strategies:

  1. Client-Side Routing
    • The client itself queries the service discovery service to find the right instance.
    • The client decides which instance to call.
    • Example: A web app calls the ExerciseService. The client asks the service registry: “Which IP is available?” and sends the request there.
  2. Server-Side Routing
    • The service discovery infrastructure includes a reverse proxy.
    • Clients always send requests to the proxy, which forwards them to the correct instance.
    • Example: A mobile app requests NotificationService. The proxy automatically picks a healthy instance and routes the request.

Key Points to Remember