What is Service Discovery and how does Eureka work?

What is Service Discovery?

In microservices, service instances:

  • Scale up/down dynamically

  • Change IPs frequently

๐Ÿ‘‰ Service Discovery helps services find each other dynamically without hard-coded URLs.


๐Ÿ”น Types of Service Discovery

1๏ธโƒฃ Client-side Discovery

  • Client asks registry for service instances

  • Client chooses one (load balancing)

๐Ÿ“Œ Example: Eureka

2๏ธโƒฃ Server-side Discovery

  • Load balancer queries registry

  • Client unaware of services

๐Ÿ“Œ Example: Kubernetes Service


๐Ÿ”น What is Netflix Eureka?

Eureka is a service registry where:

  • Services register themselves

  • Other services discover them

It is commonly used with Spring Cloud Netflix.


๐Ÿ”น Eureka Architecture

Service A โ”€โ”€registerโ”€โ”€โ–ถ Eureka Server
Service B โ”€โ”€registerโ”€โ”€โ–ถ Eureka Server

Service A โ”€โ”€discoverโ”€โ”€โ–ถ Eureka โ”€โ”€returnsโ”€โ”€โ–ถ Service B instance


๐Ÿ”น How Eureka Works (Step-by-Step)

  1. Eureka Server starts

  2. Microservices register with Eureka

  3. Services send heartbeats every 30 seconds

  4. Eureka removes unhealthy services

  5. Clients fetch registry & cache it locally

  6. LoadBalancer selects instance


๐Ÿ”น Why Eureka is Highly Available?

  • Clients cache registry

  • Even if Eureka goes down โ†’ services still talk

  • Self-preservation mode prevents mass eviction


๐Ÿ”น Example Configuration

Eureka Server

eureka:
client:
register-with-eureka: false
fetch-registry: false

Eureka Client

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka

๐Ÿ”น Eureka vs Consul

Feature Eureka Consul
Health Check Heartbeat Active
DNS Support โŒ โœ…
KV Store โŒ โœ…
Service Mesh โŒ โœ…

๐Ÿ”น Real-World Example

๐Ÿ’ก Payment Service scales from 2 โ†’ 10 instances
Eureka automatically updates registry
Clients start calling new instances


โญ Interview One-Liner

โ€œEureka enables dynamic service discovery by allowing services to register and discover each other without hard-coded endpoints.โ€


๐Ÿ”น Common Follow-Up Questions

  • What is Eureka self-preservation mode?

  • How does load balancing work with Eureka?

  • Eureka vs Kubernetes Service Discovery?

Leave a Reply