codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🏗️

System Design

21 / 49 topics
19Microservices Architecture20Monolithic vs Microservices21Service Discovery22API Gateway
Tutorials/System Design/Service Discovery
🏗️System Design

Service Discovery

Updated 2026-05-15
10 min read

Service Discovery

Introduction

In the realm of microservices architecture, service discovery is a critical component that enables services to locate and communicate with each other. As applications grow in complexity, managing service interactions becomes increasingly challenging. Service discovery helps maintain loose coupling between services, ensuring they can dynamically discover and communicate with one another without hardcoding endpoints.

Concept

Service discovery involves the process by which services identify and locate other services within a distributed system. This is essential for enabling communication and collaboration among microservices. There are two primary approaches to service discovery:

  1. Client-Side Discovery: The client (service consumer) is responsible for discovering the location of the service provider.
  2. Server-Side Discovery: A dedicated server or registry acts as an intermediary, providing clients with the necessary information to locate and communicate with services.

Techniques for Service Discovery

1. DNS-Based Service Discovery

DNS-based service discovery leverages the Domain Name System (DNS) to resolve service names to IP addresses. This method is simple and widely supported but can introduce latency due to DNS propagation delays.

Example

# Example of a DNS record for a microservice
my-service.example.com.  IN  A  192.168.1.100

2. Consul

Consul is an open-source tool that provides service discovery, configuration management, and multi-datacenter networking functionalities. It uses a distributed key-value store to manage services.

Example

# Registering a service with Consul
curl --request PUT --data '{"id": "my-service", "name": "my-service", "address": "192.168.1.100", "port": 8080}' http://localhost:8500/v1/agent/service/register

3. Eureka

Eureka is a service registry developed by Netflix that provides service discovery for microservices. It allows services to register themselves and discover other services.

Example

# Example of an Eureka configuration file (eureka-client.yml)
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4. Kubernetes Service Discovery

In a Kubernetes environment, services can discover each other using the DNS-based service discovery provided by Kubernetes itself.

Example

# Creating a Kubernetes service
kubectl expose deployment my-service --type=ClusterIP --name=my-service-svc

Practical Examples

Using Consul for Service Discovery

Let's walk through an example of setting up Consul and registering a service.

  1. Install Consul:

    $ wget https://releases.hashicorp.com/consul/1.10.3/consul_1.10.3_linux_amd64.zip
    $ unzip consul_1.10.3_linux_amd64.zip
    $ sudo mv consul /usr/local/bin/
    
  2. Start Consul Agent:

    $ consul agent -dev
    
  3. Register a Service:

    curl --request PUT --data '{"id": "my-service", "name": "my-service", "address": "192.168.1.100", "port": 8080}' http://localhost:8500/v1/agent/service/register
    
  4. Query the Service:

    $ curl http://localhost:8500/v1/catalog/service/my-service
    
    [
      {
        "ID": "my-service",
        "Node": "node-1",
        "Address": "192.168.1.100",
        "Datacenter": "dc1",
        "TaggedAddresses": {},
        "ServiceName": "my-service",
        "ServiceTags": [],
        "ServiceAddress": "",
        "ServicePort": 8080,
        "CreateIndex": 5,
        "ModifyIndex": 5
      }
    ]
    

Using Kubernetes for Service Discovery

Let's create a simple microservice and expose it using Kubernetes.

  1. Deploy a Microservice:

    # my-service.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-service
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: my-service
      template:
        metadata:
          labels:
            app: my-service
        spec:
          containers:
          - name: my-service
            image: nginx
            ports:
            - containerPort: 80
    
  2. Apply the Deployment:

    $ kubectl apply -f my-service.yaml
    
  3. Expose the Service:

    # my-service-svc.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service-svc
    spec:
      selector:
        app: my-service
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: ClusterIP
    
  4. Apply the Service:

    $ kubectl apply -f my-service-svc.yaml
    
  5. Verify Service Discovery:

    $ kubectl get svc
    
    NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    my-service-svc  ClusterIP   10.96.0.1       <none>        80/TCP    1m
    

PreviousMonolithic vs MicroservicesNext API Gateway

Recommended Gear

Monolithic vs MicroservicesAPI Gateway