Skip to main content
Service discovery is a critical component in microservices architecture, enabling services to find and communicate with each other dynamically without hard-coded addresses.

Core Concepts

Service Registry Architecture

A service registry maintains a catalog of available services and their instances: Key Information Stored:
  • IP addresses and ports
  • Service names and versions
  • Health status
  • Metadata (region, tags, capabilities)

Implementation Comparison

Spring Cloud Eureka

Origin: Netflix (now maintained by Spring)CAP Model: AP (Availability + Partition Tolerance)Architecture: Client-Server with peer-to-peer replication

Server Capabilities

Service Registration

Stores service metadata in a unified registry. Clients register on startup.

Registry Table

Provides service lists to clients. Clients cache locally and refresh every 30 seconds.

Service Eviction

Removes instances that haven’t sent heartbeats for 90 seconds (if not in self-preservation).

Self-Preservation

Protects registry during network instability. Doesn’t remove instances if 85% fail heartbeats within 15 minutes.

Client Operations

OperationIntervalDescription
RegisterOn startupSends service info (IP, port, metadata)
Renew (Heartbeat)Every 30sHTTP request to confirm health
Fetch RegistryEvery 30sUpdates local service cache
CancelOn shutdownGracefully deregisters service

Workflow

1

Server startup

Eureka Server starts and waits for registrations
2

Provider registration

Service provider registers with server on startup
3

Heartbeat mechanism

Provider sends heartbeat every 30 seconds via HTTP
4

Health monitoring

Server checks if heartbeat missing for 90s:
  • If less than 85% healthy in 15min → Self-preservation mode
  • If 85% or more healthy → Evict unhealthy instance
5

Consumer discovery

Consumer fetches and caches service list, refreshes every 30s
6

Remote invocation

Consumer calls provider using cached info (default: round-robin via Ribbon)
7

Graceful shutdown

Provider sends deregister request on shutdown

High Availability Cluster

Cluster Architecture: Peer-to-peer replication
  • No master/slave distinction
  • All nodes are equal
  • Asynchronous data replication
  • Eventually consistent (AP model)
Resilience: If one node fails:
  • Clients continue working with cached service lists
  • Other nodes handle incoming requests
  • Failed node replicates latest data on recovery
Eureka 2.x Status: The 2.x branch is no longer maintained, but 1.x is actively supported. Spring Cloud uses 1.x and can easily switch to alternatives (ZooKeeper, Consul, Nacos).

Decision Matrix

Best for:
  • Spring Cloud microservices
  • Small to medium service counts (less than 10,000 instances)
  • Need for high availability
  • Simple setup requirements
Avoid when:
  • Need strong consistency guarantees
  • Massive scale (>10,000 instances)
  • Require configuration management
Best for:
  • Distributed coordination (leader election, locks)
  • Configuration management
  • Hadoop/HBase ecosystems
  • Strong consistency requirements
Avoid when:
  • Availability is critical
  • Cannot tolerate 30-120s downtime during elections
  • Primary use case is service discovery
Best for:
  • Large-scale deployments (10,000+ instances)
  • Need both service discovery AND config management
  • Want flexible CAP model (switch between AP/CP)
  • Kubernetes environments
  • Dubbo or Spring Cloud ecosystems
Avoid when:
  • Team unfamiliar with Alibaba ecosystem
  • Simple use cases (overhead not justified)
Best for:
  • Service Mesh architectures
  • Multi-datacenter deployments
  • Need for built-in health checks
  • HashiCorp ecosystem integration
Avoid when:
  • Using Java exclusively (Go-based, harder debugging)
  • Team lacks Go language expertise

Comparison Table

FeatureEurekaZooKeeperNacosConsul
CAP ModelAPCPAP & CPCP
LanguageJavaJava/CJavaGo
Health CheckClient heartbeatSocket keep-aliveHTTP heartbeatMultiple options
Watch SupportLong pollingPushPush/PullLong polling
Scale Limit~10K instancesMedium100K+ instancesLarge
UI DashboardBasicNoneRichRich
Spring CloudNativeSupportedSupportedSupported
Config CenterNoNoYesYes
K8s IntegrationLimitedLimitedExcellentExcellent
Operational ComplexityLowMediumMediumMedium-High

Implementation Example

// Provider registration
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
# application.yml
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90

Design Considerations

Client-Side Caching

Why it matters:
  • Reduces registry load
  • Improves performance
  • Provides fallback during registry outage
Best practices:
  • Cache service lists locally
  • Refresh periodically (30s typical)
  • Handle cache invalidation properly

Network Partitions

Scenarios to handle:
  • Registry server unreachable
  • Service instance unreachable
  • Split-brain in clustered registries
Strategies:
  • Client-side circuit breakers
  • Retry with exponential backoff
  • Use health checks actively

Multi-Datacenter

Considerations:
  • Cross-region latency
  • Data consistency across DCs
  • Failover strategies
Patterns:
  • Region-aware load balancing
  • Prefer local services
  • Registry per datacenter

Security

Protect your registry:
  • Enable authentication/authorization
  • Use TLS for communication
  • Implement rate limiting
  • Network segmentation
Nacos: Built-in auth Eureka: Add Spring Security

Load Balancing

Learn how to distribute traffic across discovered services

Distributed Systems

Understand broader distributed systems concepts

Build docs developers (and LLMs) love