Service Discovery & Configuration Management Questions
Service discovery and configuration management within distributed systems. Covers runtime service lookup patterns (service registries, DNS-based discovery, and Kubernetes service discovery), health checks, load balancing, and centralizing configuration across services. Includes dynamic configuration, feature flags, secret management, versioned configuration, rollout strategies, and related operational concerns such as security, consistency, and observability. Topics span implementations with tools like Consul, Etcd, Zookeeper, as well as cloud-native and IaC approaches for microservices architectures.
Sample Answer
Sample Answer
Sample Answer
import threading
import time
from typing import List, Optional
class ClientLB:
def __init__(self, endpoints: List[str], strategy: str = "round_robin", max_failures: int = 3, cooldown: float = 30.0):
self.lock = threading.Lock()
self.endpoints = list(endpoints)
now = time.time()
# state per endpoint
self.state = {e: {"active": 0, "failures": 0, "unhealthy_until": 0.0, "last_used": now} for e in self.endpoints}
self.rr_index = 0
if strategy not in ("round_robin", "least_conn"):
raise ValueError("strategy must be 'round_robin' or 'least_conn'")
self.strategy = strategy
self.max_failures = max_failures
self.cooldown = cooldown
def update_endpoints(self, endpoints: List[str]):
with self.lock:
cur = set(self.endpoints)
new = set(endpoints)
for e in new - cur:
self.state[e] = {"active":0,"failures":0,"unhealthy_until":0.0,"last_used":time.time()}
for e in cur - new:
self.state.pop(e, None)
self.endpoints = list(endpoints)
self.rr_index %= max(1, len(self.endpoints))
def _is_healthy(self, e: str, now: float) -> bool:
s = self.state.get(e)
return s is not None and s["unhealthy_until"] <= now
def choose(self) -> Optional[str]:
now = time.time()
with self.lock:
healthy = [e for e in self.endpoints if self._is_healthy(e, now)]
if not healthy:
# all unhealthy: pick the one with earliest unhealthy_until (will break cooldown)
cand = min(self.endpoints, key=lambda x: self.state[x]["unhealthy_until"], default=None)
if cand is None:
return None
chosen = cand
else:
if self.strategy == "round_robin":
n = len(self.endpoints)
start = self.rr_index % n
chosen = None
for i in range(n):
idx = (start + i) % n
e = self.endpoints[idx]
if self._is_healthy(e, now):
chosen = e
self.rr_index = (idx + 1) % n
break
if chosen is None:
chosen = healthy[0]
else: # least_conn
chosen = min(healthy, key=lambda x: (self.state[x]["active"], self.state[x]["last_used"]))
# increment active and update last_used
self.state[chosen]["active"] += 1
self.state[chosen]["last_used"] = now
return chosen
def report_result(self, endpoint: str, success: bool):
now = time.time()
with self.lock:
if endpoint not in self.state:
return
s = self.state[endpoint]
# decrement active but never below 0
s["active"] = max(0, s["active"] - 1)
if success:
s["failures"] = 0
s["unhealthy_until"] = 0.0
else:
s["failures"] += 1
if s["failures"] >= self.max_failures:
s["unhealthy_until"] = now + self.cooldown
# helper for testing/metrics
def get_state(self):
with self.lock:
return {e: dict(self.state[e]) for e in self.endpoints}Sample Answer
Sample Answer
Unlock Full Question Bank
Get access to hundreds of Service Discovery & Configuration Management interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.