Architecture
Service Discovery Explained: How Services Find Each Other Without Hardcoded IPs
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيWhen you have one application server and one database, the database address lives in a config file and nobody thinks about it again. Add three API instances, two workers, a cache cluster and a payment service, then start replacing servers during deployments, and hardcoded IPs become a liability. A new instance starts and nobody sends it traffic; an old one is retired and clients keep calling a dead address. Service discovery is the mechanism that answers one question reliably: where are the healthy instances of service X right now?
The building blocks
Every service discovery setup has three parts:
- A registry: the source of truth listing services, their instances, addresses and ports. Examples include Consul, etcd, ZooKeeper, and the Kubernetes API with its Endpoints objects.
- Registration: how instances get into the registry and leave it. Either the service registers itself on startup (self-registration), or a separate agent or orchestrator registers it (third-party registration).
- Health checking: how the registry learns that an instance has died or become unhealthy, so it stops handing out that address.
Without health checks, a registry is just a list of addresses that slowly goes stale.
Client-side vs server-side discovery
| Client-side discovery | Server-side discovery | |
|---|---|---|
| How it works | Client queries the registry and picks an instance itself | Client calls a fixed address; a load balancer or proxy queries the registry |
| Examples | Consul client libraries, gRPC with a resolver, Netflix Eureka style | Nginx or HAProxy fed by the registry, Kubernetes Services, cloud load balancers |
| Pros | No extra hop, client can do smart balancing | Clients stay simple and language-agnostic |
| Cons | Discovery logic in every client and every language | Extra component to run and keep highly available |
For small teams, server-side discovery through a reverse proxy or DNS is usually the simplest starting point. Read more about the proxy side in our load balancer guide.
DNS-based discovery
DNS is the oldest discovery system and still a good one. Options include:
- A or AAAA records with multiple addresses per name, giving basic round-robin.
- SRV records, which carry port, priority and weight in addition to the host:
_api._tcp.example.internal. 30 IN SRV 10 50 8080 api-1.example.internal.
_api._tcp.example.internal. 30 IN SRV 10 50 8080 api-2.example.internal.- Registry-backed DNS, where Consul answers DNS queries on port 8600 and only returns healthy instances:
dig @127.0.0.1 -p 8600 api.service.consul SRVThe catch is caching. Clients and resolvers cache answers for the TTL, and some runtimes cache longer than they should. Keep TTLs short (5 to 30 seconds) for dynamic services, and check how your language runtime handles DNS caching.
A practical Consul setup
Consul is a popular choice outside Kubernetes because it combines a registry, health checks and DNS. Run three Consul servers for quorum, so you are not exposed to split-brain, and a lightweight agent on every machine. Register a service with a health check by placing a file in the agent's config directory, for example /etc/consul.d/api.hcl:
service {
name = "api"
port = 8080
tags = ["v2"]
check {
http = "http://127.0.0.1:8080/health"
interval = "10s"
timeout = "2s"
}
}Then reload with consul reload and verify with consul catalog services. To feed Nginx, use consul-template to render an upstream block from healthy instances:
upstream api {
{{- range service "api" }}
server {{ .Address }}:{{ .Port }};
{{- end }}
}Whenever instances change, consul-template rewrites the file and runs a command you configure, such as systemctl reload nginx, giving you server-side discovery without changing application code.
Simpler options when you are small
Not everyone needs a dedicated registry:
- Docker Compose or Swarm: containers on the same network reach each other by service name through Docker's built-in DNS.
- Kubernetes: Services and CoreDNS give every service a stable name such as
api.default.svc.cluster.local. - Private networking plus static config: with a handful of long-lived servers on a private network, managed by Ansible or another config tool, a templated config file can be perfectly adequate.
Move to a dedicated registry when instances change often, when you autoscale, or when manual updates start causing outages.
Checklist for reliable discovery
- Every service exposes a cheap
/healthendpoint that checks its own critical dependencies. - Health checks have sensible intervals and timeouts, and remove instances quickly after repeated failures.
- Instances deregister cleanly on shutdown and drain connections first.
- DNS TTLs are short for dynamic services.
- The registry itself is highly available: three or five servers, spread across hosts.
- Discovery traffic stays on private networks; protect Consul and etcd with ACLs and TLS.
- Clients retry another instance on connection failure instead of failing the request.
Running a three-node registry on small VPSes in the same region keeps latency low; plans are on /pricing.
FAQ
Do I need service discovery for a single server?
No. If everything runs on one server, or on a few servers that rarely change, static configuration is simpler and has fewer moving parts. Discovery becomes valuable when instances come and go through deployments, scaling or failures.
What is the difference between service discovery and a load balancer?
Service discovery tells you which instances exist and are healthy. A load balancer distributes requests across instances. They are often combined: the load balancer reads its list of backends from the discovery system, which is what server-side discovery means.
Is plain DNS good enough for service discovery?
Often, yes, especially with short TTLs and a DNS server backed by health checks. Plain DNS without health checks will keep returning dead addresses until someone edits the records, and client-side caching can delay changes, so check both before relying on it.