Service mesh — infrastructure layer for service-to-service communication.
Key components:
1// Simple service discovery2class ServiceRegistry {3 constructor() {4 this.services = new Map();5 }67 register(name, instance) {8 if (!this.services.has(name)) {9 this.services.set(name, []);10 }11 this.services.get(name).push({12 ...instance,13 lastHeartbeat: Date.now(),14 });15 }1617 discover(name) {18 const instances = this.services.get(name) || [];19 const healthy = instances.filter(20 i => Date.now() - i.lastHeartbeat < 1000021 );22 // Round-robin23 return healthy[Math.floor(Math.random() * healthy.length)];24 }2526 deregister(name, instanceId) {27 const instances = this.services.get(name) || [];28 this.services.set(name,29 instances.filter(i => i.id !== instanceId)30 );31 }32}3334// Client-side load balancing35async function callService(serviceName) {36 const instance = registry.discover(serviceName);37 if (!instance) throw new Error("No healthy instances");38 return fetch(`http://${instance.host}:${instance.port}`);39}
Production tools: Istio, Linkerd, Consul.