Back to results for “Architecture”
Node.js › Microservices Architecture with Node.js

How would you handle service discovery in a Node.js microservices architecture, so services can find each other's network locations dynamically?

Advanced

Answer

In a dynamic environment (containers being created and destroyed, auto-scaling adjusting instance counts), hardcoding a downstream service's IP address doesn't work -- service discovery mechanisms include DNS-based discovery (Kubernetes' built-in DNS resolves a service name to the current set of healthy instance IPs automatically), a dedicated service registry (like Consul, where services register themselves on startup and deregister on shutdown), or a service mesh (like Istio, which handles discovery and routing transparently at the network layer).
// Kubernetes DNS-based service discovery: no IP addresses hardcoded anywhere
const response = await fetch('http://inventory-service.default.svc.cluster.local/products/123');
// Kubernetes automatically resolves 'inventory-service' to a currently healthy pod IP
Real-world example A team deploying to Kubernetes relies entirely on Kubernetes' built-in DNS-based service discovery, calling downstream services by their stable service name rather than any specific pod's IP address, which changes constantly as pods are created and destroyed during routine scaling and deployments.

Common follow-ups: How does DNS-based discovery handle load balancing across multiple healthy instances of the same service?;What additional capabilities does a full service mesh provide beyond basic discovery, like automatic retries and circuit breaking?