Tutorials Microservices & Event-Driven Architecture (EDA) Mastery
Service Discovery and Health Checks in .NET
On this page
Dynamic Infrastructure
In a cloud environment, IP addresses are ephemeral. Containers die and restart constantly. You need a way for Service A to find Service B without hard-coding URLs.
1. Service Discovery (Consul / K8s DNS)
In Kubernetes, we use **CoreDNS**. You just call `http://payment-service` and K8s handles finding the right IP of a healthy instance. In non-K8s environments, tools like **Consul** allow services to 'Register' themselves and their health status in a central registry.
2. Health Checks: The Vital Signs
.NET has a built-in Health Check middleware. It's not just "Is the app running?". A real health check verifies:
- **Database Connectivity:** Can I ping SQL?
- **Memory Usage:** Is the pod near its limit?
- **External APIs:** Is Stripe/SendGrid reachable?
4. Interview Mastery
Q: "What is the difference between a 'Liveness' probe and a 'Readiness' probe?"
Architect Answer: "A **Liveness** probe tells K8s: 'Am I alive or dead?'. If it fails, K8s kills and restarts the container. A **Readiness** probe tells K8s: 'I'm alive, but I'm busy (e.g., heating up cache or migrating DB)'. If it fails, K8s keeps the container alive but stops sending it traffic. Using only Liveness probes can lead to 'Restart Loops' where a slow-starting service is killed before it can finish booting."