Hackforge Academy

Category: python

What is Service Discovery?

Published on 22 Aug 2026

Explanation

Service discovery is the process of automatically finding the network location of services in a microservices architecture. In a small application, a service might call another service using a fixed URL such as http://localhost:8001. In a production environment, service instances can start, stop, scale, or move to different machines, making fixed URLs difficult to manage. A service registry stores information about available service instances, such as service name, host, port, and health status. Services can then discover each other dynamically instead of hardcoding network addresses.

Code:

# Without Service Discovery
PRODUCT_SERVICE = 'http://localhost:8001'


# With Service Discovery
# Order Service asks the registry:
# 'Where is the Product Service?'
#
# Registry response:
# Product Service
# -> 10.0.0.15:8001
# -> 10.0.0.16:8001

Explanation

A service registry is a central location that keeps track of available microservice instances. When a service starts, it registers itself with the registry. When a service stops or becomes unhealthy, its registration can be removed or marked unavailable. Other services query the registry to discover the current location of the service they need. Popular service discovery technologies include Consul, Eureka, Kubernetes Service Discovery, and cloud-native service registries.

Code:

# Simple in-memory service registry example

service_registry = {
    'product-service': [
        'http://localhost:8001',
        'http://localhost:8002'
    ],
    'order-service': [
        'http://localhost:8003'
    ]
}


product_instances = service_registry.get(
    'product-service', []
)

print(product_instances)

Explanation

A Python microservice can register its location with a service registry when it starts. FastAPI provides application startup and shutdown mechanisms that can be used for initialization and cleanup tasks. In a real implementation, the registration request would be sent to a service registry such as Consul. The service can register its name, address, port, and health-check endpoint so that other services can discover it.

Code:

from fastapi import FastAPI

app = FastAPI()

SERVICE_NAME = 'product-service'
SERVICE_HOST = 'localhost'
SERVICE_PORT = 8001


@app.on_event('startup')
async def register_service():
    print('Registering service...')
    print('Name:', SERVICE_NAME)
    print('Host:', SERVICE_HOST)
    print('Port:', SERVICE_PORT)


@app.get('/health')
def health():
    return {'status': 'healthy'}


@app.get('/products')
def products():
    return {'products': []}

Explanation

After a service has registered itself, another microservice can query the registry to find available instances. The calling service does not need to know the exact host and port beforehand. The registry returns one or more healthy instances, and the client can send a request to the selected instance. Service discovery can be combined with load balancing so that requests are distributed across multiple healthy instances.

Code:

import httpx

SERVICE_REGISTRY = {
    'product-service': [
        'http://localhost:8001',
        'http://localhost:8002'
    ]
}


def discover_service(service_name):
    instances = SERVICE_REGISTRY.get(service_name, [])

    if not instances:
        raise Exception(
            f'{service_name} not available'
        )

    return instances[0]


async def get_products():
    service_url = discover_service('product-service')

    async with httpx.AsyncClient() as client:
        response = await client.get(
            f'{service_url}/products'
        )

    return response.json()

Explanation

Consul is a popular service discovery and configuration tool that can maintain a registry of healthy service instances. A Python service can register itself with Consul and other services can query Consul to find available instances. Consul can also perform health checks and remove unhealthy instances from discovery results. In larger Python microservices systems, service discovery can be combined with FastAPI, HTTPX, Docker, Kubernetes, load balancing, and API gateways.

Code:

import requests

CONSUL_URL = 'http://localhost:8500'

# Register Product Service
registration = {
    'ID': 'product-service-1',
    'Name': 'product-service',
    'Address': '127.0.0.1',
    'Port': 8001,
    'Check': {
        'HTTP': 'http://127.0.0.1:8001/health',
        'Interval': '10s'
    }
}

requests.put(
    f'{CONSUL_URL}/v1/agent/service/register',
    json=registration
)

# Discover healthy Product Service instances
response = requests.get(
    f'{CONSUL_URL}/v1/health/service/product-service',
    params={'passing': 'true'}
)

for service in response.json():
    address = service['Service']['Address']
    port = service['Service']['Port']
    print(f'Product Service: {address}:{port}')

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community