1. Message Queues (Kafka)
In monolithic applications, modules call each other directly. If one fails, the whole process fails. Microservices use **Message Queues** to operate asynchronously. For example, an OrderService writes an event to Kafka, and an EmailService reads it eventually, decoupling logic.
typescript
// Decoupled Microservice Architecture logic
class OrderService {
processCheckout(orderData: any) {
// ... Save order to DB ...
// Push event to Kafka instead of calling EmailService directly
kafkaProducer.send({
topic: 'orders_topic',
messages: [{ value: JSON.stringify({ event: 'ORDER_CREATED', data: orderData }) }]
});
}
}2. Rate Limiting (Token Bucket)
To prevent DDoS attacks or API abuse, rate limiters are deployed. The most common algorithmic implementation is the Token Bucket.
typescript
class TokenBucket {
private capacity: number;
private tokens: number;
private refillRatePerSecond: number;
private lastRefillTimestamp: number;
constructor(capacity: number, refillRate: number) {
this.capacity = capacity;
this.tokens = capacity;
this.refillRatePerSecond = refillRate;
this.lastRefillTimestamp = Date.now();
}
public allowRequest(): boolean {
this.refillTokens();
if (this.tokens > 0) {
this.tokens--;
return true;
}
return false; // Rate limit exceeded!
}
private refillTokens() {
const now = Date.now();
const secondsPassed = (now - this.lastRefillTimestamp) / 1000;
const newTokens = secondsPassed * this.refillRatePerSecond;
this.tokens = Math.min(this.capacity, this.tokens + newTokens);
this.lastRefillTimestamp = now;
}
}Take the Phase Interview
Solidify your knowledge. We will ask you 5 random highly-technical questions from the HLD Phase 3: Specialized Infrastructure bucket. A Staff Engineer AI will strictly evaluate your answers.