skip to content

Netflix Eureka

Eureka is a registry where instances register and renew leases, clients cache the registry locally, and self-preservation stops mass eviction during a network blip. Interviewers ask about that cache and about stale instances after a hard crash.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

questions

5

What is Netflix Eureka, and what do @EnableEurekaServer and @EnableEurekaClient do?

level: juniorimportance: must knowfreq 70%

answer

  1. registry = name -> live instances
  2. server annotation vs client starter
  3. @EnableEurekaClient (Eureka) vs @EnableDiscoveryClient (portable)
  4. app name is the lookup key
  5. annotations optional since starter auto-configures

basics

~20 s

Eureka is a service registry: services register themselves so others can find them by name instead of a hardcoded address. @EnableEurekaServer starts the registry server; @EnableEurekaClient makes an app register with and query that registry.

solid answer

~40 s

Eureka (from Spring Cloud Netflix) is a service-discovery registry. Instead of hardcoding host:port, each service registers itself under a logical name and looks others up by that name, which enables dynamic scaling and location transparency. @EnableEurekaServer, on a Spring Boot app with the eureka-server starter, turns it into the registry that holds the list of all instances. @EnableEurekaClient (or the generic @EnableDiscoveryClient) marks a service as a client: on startup it registers its own instance (app name, IP, port, health URL) with the server and periodically fetches the registry so it can resolve other services. In modern Spring Cloud the annotation is optional — just having the Eureka client starter on the classpath auto-configures registration and discovery.

code

java · 26 lines
java
// --- Registry server ---
@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {
    public static void main(String[] args) {
        SpringApplication.run(RegistryApplication.class, args);
    }
}

// application.yml (server)
// server.port: 8761
// eureka.client.register-with-eureka: false
// eureka.client.fetch-registry: false

// --- A service that registers ---
@SpringBootApplication
@EnableDiscoveryClient // portable; @EnableEurekaClient also works, and even this is optional
public class PaymentServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentServiceApplication.class, args);
    }
}

// application.yml (client)
// spring.application.name: payment-service   <-- the discovery key
// eureka.client.service-url.defaultZone: http://localhost:8761/eureka/

go deeper

for a junior

Know Eureka is a phone book of services; server vs client roles; app name is the key.

for a middle

Explain that annotations are optional (starter auto-configures) and the difference between @EnableEurekaClient and @EnableDiscoveryClient.

for a senior

Tie discovery to client-side load balancing and standalone/HA server config (register-with-eureka/fetch-registry).

for a principal

Discuss when Eureka is redundant (Kubernetes native discovery) and the operational cost of running/maintaining a registry cluster.

## The problem Eureka solves In a microservice system, service A must call service B. Hardcoding B's `host:port` breaks the moment B scales out, moves, or restarts on a new port (common in containers/cloud). **Service discovery** replaces fixed addresses with a logical name (e.g. `payment-service`) resolved at runtime. **Netflix Eureka** is the registry that Spring Cloud Netflix wraps. ## Two roles - **Eureka Server** — the registry itself: an in-memory map of `service name -> list of live instances`. You create it by putting `spring-cloud-starter-netflix-eureka-server` on the classpath and annotating the main class with **`@EnableEurekaServer`**. - **Eureka Client** — any service that registers with and/or queries the server. You add `spring-cloud-starter-netflix-eureka-client`. Historically you annotated with **`@EnableEurekaClient`** (Eureka-specific) or **`@EnableDiscoveryClient`** (discovery-implementation-agnostic — works for Eureka, Consul, Zookeeper). ## The annotations are largely optional now Since Spring Cloud Greenwich, **auto-configuration activates purely from the client starter being on the classpath** — you don't strictly need `@EnableEurekaClient`/`@EnableDiscoveryClient`. They remain as explicit intent/documentation. `@EnableEurekaClient` is Eureka-specific; `@EnableDiscoveryClient` is the portable choice. ## What registration carries When a client registers it sends an **instance** descriptor: the **application name** (`spring.application.name`, used as the lookup key, uppercased as VIP), an **instance id**, **IP/hostname and port**, a **health-check/status URL**, and arbitrary **metadata**. Consumers fetch this list and pick an instance (usually via a client-side load balancer like Spring Cloud LoadBalancer). ## Typical config ```yaml eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` The server itself is just a Boot app on port 8761 (by convention) that usually sets `register-with-eureka: false` and `fetch-registry: false` for a standalone node. ## Gotchas - The **app name** (not the instance id) is the discovery key; two instances of the same service share one name. - A bare Eureka server is still a Eureka client of itself by default — disable self-registration for standalone dev, or point peers at each other for HA. - `@EnableEurekaServer` and the client starter are different dependencies; don't confuse them. ## When to use Use Eureka when you run multiple, dynamically-scaled service instances and want name-based, client-side load-balanced calls without an external DNS/load-balancer per service. For pure Kubernetes deployments, native K8s DNS/Services often replace Eureka.

  • What is the difference between @EnableEurekaClient and @EnableDiscoveryClient?
    @EnableEurekaClient is Eureka-specific; @EnableDiscoveryClient is discovery-implementation-agnostic (Eureka, Consul, Zookeeper). Both are largely optional today because the client starter auto-configures registration/discovery — the difference is really about portability of intent.
  • How does a consumer actually call a service by name?
    It resolves the name against the client's cached registry, then a client-side load balancer (Spring Cloud LoadBalancer, formerly Ribbon) picks an instance. With a load-balanced RestTemplate/WebClient or an OpenFeign client you just use http://payment-service/... and the name is expanded to a real address.

context

open as a page

Explain instance registration, lease renewal heartbeats, and how the server evicts dead instances in Eureka.

level: middleimportance: must knowfreq 65%

basics

~20 s

On startup a client registers an instance and gets a lease. It then sends a heartbeat (renews the lease) every 30 seconds. If the server gets no heartbeat within the lease-expiration window (90s), the lease expires and an eviction task removes that instance from the registry.

open as a page

How does the client-side registry cache work in Eureka, and what staleness does it introduce?

level: middleimportance: should knowfreq 45%

basics

~20 s

Each Eureka client keeps a local copy of the registry and refreshes it periodically (every 30s by default), usually by fetching only the changes (deltas). Lookups hit this local cache, not the server, so discovery is fast but can be up to ~30s stale.

open as a page

What is Eureka's self-preservation mode, why does it exist, and what problems can it cause?

level: seniorimportance: should knowfreq 50%

basics

~20 s

If the server suddenly stops receiving enough heartbeats (below ~85% of expected), it assumes a network problem rather than mass instance death and stops evicting instances — keeping them registered to avoid wiping out a healthy registry. The downside: it can keep dead instances listed during a real outage.

open as a page

Where does Eureka sit on the CAP spectrum, and how do its design choices (peer replication, caching, self-preservation) reflect that? When might you not use it?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Eureka is an AP system: during a network partition it stays available and returns possibly-stale data rather than blocking to guarantee consistency. Everything about it — peer replication without consensus, client caches, self-preservation — favors availability over a perfectly consistent registry.

open as a page