Service Mesh Architecture
Build scalable, resilient distributed systems using Cloudflare Workers as microservices.
Service Mesh Topology
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ API Gateway │────│ Service Router │────│ Auth Service │
│ (Worker) │ │ (Worker) │ │ (Worker) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└────────────────────────┴────────────────────────┘
Service Discovery
│
┌─────────────────┐ ┌─────────────────┐
│ User Service │────│Product Service │
│ (Worker) │ │ (Worker) │
└─────────────────┘ └─────────────────┘
Service Router Implementation
export default {
async fetch(request, env) {
const url = new URL(request.url);
// Service discovery
const services = {
'/api/users': env.USER_SERVICE,
'/api/products': env.PRODUCT_SERVICE,
'/api/auth': env.AUTH_SERVICE
};
// Route to appropriate service
for (const [path, serviceUrl] of Object.entries(services)) {
if (url.pathname.startsWith(path)) {
return await routeToService(request, serviceUrl, path);
}
}
return new Response('Service not found', { status: 404 });
}
};
async function routeToService(request, serviceUrl, basePath) {
const url = new URL(request.url);
const targetUrl = new URL(serviceUrl + url.pathname.replace(basePath, ''));
// Forward headers and body
const response = await fetch(targetUrl, {
method: request.method,
headers: request.headers,
body: request.body
});
return response;
}
Distributed State Management
Manage state across multiple Workers using Durable Objects and coordination patterns.
Durable Object Coordinator
export class StateCoordinator {
constructor(state, env) {
this.state = state;
this.env = env;
}
async handleSession(sessionId, data) {
// Distributed locking
const lock = await this.acquireLock(sessionId);
try {
// Update state atomically
const currentState = await this.getState(sessionId);
const newState = this.mergeState(currentState, data);
await this.setState(sessionId, newState);
return newState;
} finally {
await this.releaseLock(lock);
}
}
async acquireLock(key) {
// Implement distributed locking using Durable Objects
return await this.env.LOCK_MANAGER.acquire(key);
}
}
Performance Optimization Patterns
Edge Caching Strategy
// Smart caching with stale-while-revalidate
async function handleRequest(request, env) {
const cache = caches.default;
const cacheKey = new Request(request.url);
let response = await cache.match(cacheKey);
if (!response) {
response = await fetch(request);
response = new Response(response.body, response);
// Cache for 5 minutes, serve stale for 1 hour
response.headers.set('Cache-Control',
'max-age=300, stale-while-revalidate=3600');
await cache.put(cacheKey, response.clone());
}
return response;
}
Request Batching
// Batch multiple API calls
class RequestBatcher {
constructor(batchSize = 10, timeout = 100) {
this.batchSize = batchSize;
this.timeout = timeout;
this.queue = [];
this.timer = null;
}
async add(request) {
return new Promise((resolve, reject) => {
this.queue.push({ request, resolve, reject });
if (this.queue.length >= this.batchSize) {
this.flush();
} else if (!this.timer) {
this.timer = setTimeout(() => this.flush(), this.timeout);
}
});
}
async flush() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
const batch = this.queue.splice(0);
if (batch.length === 0) return;
try {
const results = await this.processBatch(batch.map(b => b.request));
batch.forEach((item, index) => item.resolve(results[index]));
} catch (error) {
batch.forEach(item => item.reject(error));
}
}
}
Connection Pooling
// Database connection pooling
class ConnectionPool {
constructor(maxConnections = 10) {
this.maxConnections = maxConnections;
this.connections = [];
this.waitingQueue = [];
}
async getConnection() {
if (this.connections.length > 0) {
return this.connections.pop();
}
if (this.connections.length < this.maxConnections) {
return await this.createConnection();
}
return new Promise((resolve) => {
this.waitingQueue.push(resolve);
});
}
releaseConnection(connection) {
if (this.waitingQueue.length > 0) {
const waiting = this.waitingQueue.shift();
waiting(connection);
} else {
this.connections.push(connection);
}
}
}
Enterprise Observability
Implement comprehensive monitoring, logging, and tracing across your Worker ecosystem.
Distributed Tracing
class Tracer {
constructor(serviceName) {
this.serviceName = serviceName;
this.traces = new Map();
}
startSpan(name, parentSpanId = null) {
const spanId = crypto.randomUUID();
const span = {
id: spanId,
name,
startTime: Date.now(),
parentSpanId,
tags: {},
events: []
};
this.traces.set(spanId, span);
return spanId;
}
endSpan(spanId) {
const span = this.traces.get(spanId);
if (span) {
span.endTime = Date.now();
span.duration = span.endTime - span.startTime;
// Send to tracing service
this.exportSpan(span);
}
}
addTag(spanId, key, value) {
const span = this.traces.get(spanId);
if (span) {
span.tags[key] = value;
}
}
async exportSpan(span) {
// Export to your tracing backend
await fetch('https://tracing.example.com/spans', {
method: 'POST',
body: JSON.stringify(span)
});
}
}
Advanced Security Patterns
Zero Trust Architecture
// Continuous authentication
async function authenticateRequest(request, env) {
const token = request.headers.get('Authorization');
if (!token) {
return new Response('Unauthorized', { status: 401 });
}
// Verify JWT with rotation
const payload = await verifyJWT(token, env.JWT_SECRET);
// Check token freshness (not older than 5 minutes)
if (Date.now() - payload.iat * 1000 > 300000) {
return new Response('Token expired', { status: 401 });
}
// Additional context validation
const userContext = await getUserContext(payload.sub);
if (!userContext.active) {
return new Response('Account disabled', { status: 403 });
}
return payload;
}
Rate Limiting with Redis
// Distributed rate limiting
class RateLimiter {
constructor(env, windowMs = 60000, maxRequests = 100) {
this.env = env;
this.windowMs = windowMs;
this.maxRequests = maxRequests;
}
async checkLimit(identifier) {
const key = `rate_limit:${identifier}`;
const now = Date.now();
const windowStart = now - this.windowMs;
// Use Redis for distributed counting
const requests = await this.env.REDIS.zrangebyscore(key, windowStart, now);
if (requests.length >= this.maxRequests) {
return { allowed: false, resetTime: windowStart + this.windowMs };
}
// Add current request
await this.env.REDIS.zadd(key, now, crypto.randomUUID());
// Clean old entries
await this.env.REDIS.zremrangebyscore(key, 0, windowStart);
return { allowed: true };
}
}
Advanced Deployment Strategies
Blue-Green Deployment
// Blue-green deployment with traffic shifting
class TrafficManager {
constructor(env) {
this.env = env;
this.versions = { blue: 'v1.0', green: 'v1.1' };
this.active = 'blue';
}
async shiftTraffic(version, percentage) {
// Update route configuration
await this.env.KV.put('traffic_config', JSON.stringify({
blue: version === 'blue' ? percentage : 100 - percentage,
green: version === 'green' ? percentage : 100 - percentage
}));
}
async routeRequest(request) {
const config = JSON.parse(await this.env.KV.get('traffic_config'));
const random = Math.random() * 100;
const target = random < config.blue ? 'blue' : 'green';
const workerUrl = this.env[`${target.toUpperCase()}_WORKER`];
return await fetch(`${workerUrl}${request.url.pathname}`, request);
}
}
Canary Releases
// Gradual rollout with feature flags
class FeatureFlagManager {
constructor(env) {
this.env = env;
}
async isFeatureEnabled(feature, userId) {
const rollout = await this.getRolloutPercentage(feature);
const userHash = this.hashUserId(userId);
return userHash % 100 < rollout;
}
async getRolloutPercentage(feature) {
const config = await this.env.KV.get(`feature:${feature}`);
return config ? parseInt(config) : 0;
}
hashUserId(userId) {
// Consistent hashing for canary targeting
let hash = 0;
for (let i = 0; i < userId.length; i++) {
hash = ((hash << 5) - hash + userId.charCodeAt(i)) & 0xffffffff;
}
return Math.abs(hash) % 100;
}
}
Scaling Patterns
Auto-scaling Workers
Use Durable Objects for stateful scaling and Queue for async processing.
Global Load Balancing
Leverage Cloudflare's edge network for automatic global distribution.
Circuit Breaker Pattern
Implement fault tolerance with automatic failure detection and recovery.
Additional Resources & References
📚 Official Documentation
🔬 Research & Best Practices
🛠ï¸ Tools & Frameworks
Master Enterprise Worker Development
Build scalable, secure, and performant distributed systems with advanced Cloudflare Workers patterns.
Get Started