Service Discovery

What is Service Discovery?

Service discovery is the process of automatically detecting devices and services on a network. It enables dynamic scaling, fault‑tolerance, and reduces the need for manual configuration.

Common scenarios:

Discovery Patterns

Client‑Side Discovery

The client queries a registry (e.g., Consul, etcd) and selects an instance.

var client = new HttpClient();
var address = registry.GetService("orders");
var response = await client.GetAsync($"{address}/api/orders");

Server‑Side Discovery (Proxy)

A sidecar or API gateway resolves the service name and forwards the request.

apiVersion: v1
kind: Service
metadata:
  name: orders
spec:
  selector:
    app: orders
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

Zero‑Configuration (mDNS, SSDP)

Devices broadcast their presence without a central registry.

Implementation Guides

Using Consul with .NET

  1. Install Consul agent.
  2. Add Consul.AspNetCore NuGet package.
  3. Register service in Program.cs:
builder.Host.UseConsul(options =>
{
    options.Address = new Uri("http://localhost:8500");
    options.Datacenter = "dc1";
    options.ServiceName = "orders";
    options.ServiceId = "orders-1";
});

mDNS with Node.js

const mdns = require('mdns-js');
const browser = mdns.createBrowser();

browser.on('ready', () => browser.discover());
browser.on('update', data => {
  console.log('Discovered service:', data.fullname);
});

Tools & Libraries

ToolLanguageUse‑Case
ConsulAnyDistributed KV store & service registry
EurekaJavaNetflix OSS service discovery
etcdGo/AllKey‑value store with watch capability
Bonjour (mDNS)Swift/Obj‑CZero‑conf on local networks
ZooKeeperJava/ScalaConfiguration & discovery for Hadoop ecosystem

Frequently Asked Questions

Do I need a central registry?
Not always. For small LANs, mDNS works well. Large cloud deployments benefit from a central store like Consul or etcd.
How does load balancing work?
Clients can pick instances using round‑robin, least‑connections, or custom health‑based algorithms after querying the registry.
Is discovery secure?
Yes, use TLS between clients and the registry, and enable ACLs or token‑based authentication on the registry.