Answer:
- Using async / non-blocking operations to handle more requests simultaneously
- Avoid slow DB calls in critical paths
- Using Fastify (10k RPS) instead of Express (2 - 3k RPS)
- Enable NodeJS cluster module to utilize multi-core systems
// Point 4 example
const cluster = require('cluster');
const { cpus } = require('os');
const { createServer } = require('http');
const numCPUs = cpus().length;
if (cluster.isPrimary) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
createServer((req, res) => {
res.end('Ok');
}).listen(3000);
}- DB optimization:
- Ensure indexes on query fields
- Avoid N + 1 queries
- Using cache mechanisms if possible (eg: Redis)
- Using HTTP2/3
- Reduce number of TCP connections creation
- Use HTTP/2 multiplexing
- Using Load Balancer
- Put NGINX in front of API servers
- Distributed load into multiple backend instances (scale horizontally)