Skip to content

Service Performance Optimization

What You’ll Learn: Performance profiling, optimization techniques, and scaling strategies for production services.

This tutorial teaches you to identify and fix performance bottlenecks in your services, implement caching, optimize database queries, and scale services effectively.

  1. Performance Profiling: Identify bottlenecks
  2. Database Optimization: Query optimization, indexing
  3. Caching Strategies: Redis integration, cache patterns
  4. Message Bus Optimization: Reduce latency
  5. Horizontal Scaling: Load balancing, stateless services
  • Latency: p50, p95, p99 response times
  • Throughput: Requests per second
  • Error Rate: Failed requests percentage
  • Resource Usage: CPU, memory, connections
// Bad: N+1 queries
for (const orderId of orderIds) {
const order = await OrderReadModel.findById(orderId);
results.push(order);
}
// Good: Batch query
const orders = await OrderReadModel.findByIds(orderIds);
// Cache frequently accessed data
const cacheKey = `user:${userId}`;
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);
const user = await UserReadModel.findById(userId);
await redis.set(cacheKey, JSON.stringify(user), 'EX', 3600);
return user;
@ReadModel({ tableName: 'orders' })
export class OrderReadModel {
@Index(undefined, { unique: true })
id!: string;
@Index() // Add index for frequent queries
customerId!: string;
@Index() // Composite index for range queries
createdAt!: Date;
}

Use tools like k6 or Artillery to test under load:

import http from 'k6/http';
import { check } from 'k6';
export const options = {
vus: 100,
duration: '5m',
};
export default function() {
const res = http.post('http://localhost:3003/api/orders', {
customerId: 'customer-123',
items: [{ productId: 'product-1', quantity: 1 }]
});
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
}