Introduction
As applications grow, they must handle more traffic and ensure reliability. This chapter covers scaling strategies, load balancing, and monitoring tools for Node.js applications.
1. Scaling Strategies
- Vertical Scaling: Increasing server resources (CPU, RAM).
- Horizontal Scaling: Running multiple instances of the application.
- Load Balancing: Distributing traffic across instances.
- Caching: Storing frequently accessed data.
2. Load Balancing with Nginx
- Install Nginx:
sudo apt update && sudo apt install nginx
- Configure Nginx as a reverse proxy:
server { listen 80; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
- Restart Nginx:
sudo systemctl restart nginx
3. Using PM2 for Process Management
- Install PM2:
npm install -g pm2
- Start and manage Node.js applications:
pm2 start server.js --name myapp pm2 save && pm2 startup
- Monitor running processes:
pm2 list pm2 logs
4. Implementing Caching for Performance
Using Redis for Caching
- Install Redis:
sudo apt install redis-server
- Install Redis package for Node.js:
npm install redis
- Example: Caching API Responses
const redis = require("redis"); const client = redis.createClient(); app.get("/data", async (req, res) => { const cachedData = await client.get("data"); if (cachedData) return res.json(JSON.parse(cachedData)); const freshData = await fetchDataFromDB(); client.setex("data", 3600, JSON.stringify(freshData)); res.json(freshData); });
5. Monitoring and Logging
Using Winston for Logging
- Install Winston:
npm install winston
- Configure Logging:
const winston = require("winston"); const logger = winston.createLogger({ level: "info", format: winston.format.json(), transports: [ new winston.transports.File({ filename: "app.log" }) ] });
Using Prometheus for Metrics
- Install Prometheus client:
npm install prom-client
- Expose Metrics:
const client = require("prom-client"); const collectDefaultMetrics = client.collectDefaultMetrics; collectDefaultMetrics(); app.get("/metrics", (req, res) => { res.set("Content-Type", client.register.contentType); res.end(client.register.metrics()); });
Exercises
- Configure an Nginx reverse proxy for a Node.js application.
- Set up PM2 to manage and monitor a running Node.js process.
- Implement Redis caching for API responses.
- Configure Winston for structured logging.
- Integrate Prometheus for real-time monitoring.
Conclusion
This chapter covered scaling and monitoring Node.js applications, including load balancing, caching, process management, and logging. In the next chapter, we will explore security enhancements in production environments.