Cloud Server Performance Optimization in Practice
Practical tips for cloud server performance optimization from operating system, application architecture, database, and other perspectives.
Why Performance Optimization is Needed
Cloud server performance optimization can improve resource utilization, reduce costs, and improve user experience. Proper optimization allows the same configuration to support more business.
Assess Current Status
Performance Metrics
Key Metrics:
- CPU usage
- Memory usage
- Disk I/O
- Network throughput
- Response time
Monitoring Tools:
# System monitoring
top
htop
vmstat
iostat
Operating System Optimization
1. Kernel Parameter Optimization
/etc/sysctl.conf configuration:
# Network optimization
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_fin_timeout = 30
# File descriptors
fs.file-max = 655350
2. Filesystem Optimization
Choose appropriate filesystem:
- ext4 - Stable and reliable, good compatibility
- xfs - Good large file performance
- btrfs - Supports snapshots and compression
3. Disk Scheduler
# View current scheduler
cat /sys/block/sda/queue/scheduler
# Temporary change
echo deadline > /sys/block/sda/queue/scheduler
Application Optimization
1. Web Server Optimization
Nginx optimization:
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript
application/json application/javascript;
# Cache configuration
open_file_cache max=100000 inactive=20s;
}
2. PHP Optimization
PHP-FPM configuration:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
# OPcache
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=10000
3. Node.js Optimization
Cluster mode:
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
require('./app');
}
4. Java Application Optimization
JVM parameters:
java -Xms2g -Xmx2g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-jar app.jar
Database Optimization
1. MySQL Optimization
Configuration optimization:
[mysqld]
max_connections = 500
innodb_buffer_pool_size = 4G
key_buffer_size = 256M
table_open_cache = 2000
slow_query_log = 1
long_query_time = 2
Query optimization:
-- Create index
CREATE INDEX idx_user_email ON users(email);
-- Optimize query
EXPLAIN SELECT * FROM users WHERE email = 'user@example.com';
2. Redis Optimization
Configuration optimization:
maxmemory 2gb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
Caching Strategy
1. Application Layer Cache
Memory cache:
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 600 });
cache.set('key', 'value');
2. Distributed Cache
Redis cache:
async function getData(key) {
// Check cache first
const cached = await client.get(key);
if (cached) return JSON.parse(cached);
// Cache miss, query database
const data = await db.query('SELECT * FROM table WHERE id = ?', [key]);
// Write to cache
await client.setex(key, 3600, JSON.stringify(data));
return data;
}
3. CDN Cache
HTTP cache headers:
// Express.js
app.use(express.static('public', {
maxAge: '1d',
etag: true,
lastModified: true
}));
Load Balancing
1. Nginx Load Balancing
upstream backend {
least_conn;
server backend1.example.com weight=3;
server backend2.example.com;
server backend3.example.com backup;
}
server {
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Monitoring and Diagnostics
1. Application Monitoring
PM2 monitoring:
pm2 start app.js -i max
pm2 monit
2. Log Management
Log rotation:
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
rotate 14
compress
notifempty
create 0640 www-data adm
}
Performance Testing
1. Stress Testing
Apache Bench:
ab -n 1000 -c 10 https://example.com/
wrk:
wrk -t12 -c400 -d30s https://example.com/
Summary
Performance optimization is an ongoing process requiring:
- Monitor - Establish comprehensive monitoring system
- Analyze - Identify performance bottlenecks
- Optimize - Targeted optimization
- Test - Verify optimization results
- Iterate - Continuously improve optimization plan
Remember: Premature optimization is the root of all evil. Make code work first, then make it fast.
Related Reading: