Get the most out of your cloud instances with these optimization techniques for CPU, memory, disk, and network performance.
CPU Optimization
Nginx Worker Optimization
# /etc/nginx/nginx.conf
# Set workers to CPU core count
worker_processes auto;
# Optimize worker connections
events {
worker_connections 1024;
use epoll;
}
# Enable multi-threading
worker_rlimit_nofile 65535;Memory Optimization
Enable Swap (with caution)
# Create 2GB swap file
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
# Make permanent
echo '/swapfile none swap sw 0 0' >> /etc/fstab
# Optimize swap usage (lower swappiness)
sysctl vm.swappiness=10
echo 'vm.swappiness=10' >> /etc/sysctl.confNote: Swap is slower than RAM. Use it as emergency overflow, not as primary memory. If you're constantly using swap, resize to a larger instance.
MySQL Memory Tuning
# /etc/mysql/my.cnf
[mysqld]
# For 2GB RAM instance
innodb_buffer_pool_size = 1G
key_buffer_size = 256M
max_connections = 150
query_cache_size = 64MDisk I/O Optimization
Enable noatime
# Edit /etc/fstab
# Change 'defaults' to 'defaults,noatime'
UUID=xxx / ext4 defaults,noatime 0 1
# Remount
mount -o remount /For databases and high I/O applications, always use SSD-backed storage. The performance difference is dramatic compared to HDD.
Network Optimization
TCP Tuning
# /etc/sysctl.conf
# Increase TCP buffer sizes
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Enable TCP window scaling
net.ipv4.tcp_window_scaling = 1
# Increase connection backlog
net.core.somaxconn = 65535
# Apply changes
sysctl -pEnable HTTP/2 and Compression
# Nginx configuration
server {
listen 443 ssl http2;
# Enable gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/json application/javascript;
}Caching Strategies
Web Server Caching
# Nginx static file caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}Redis Setup
# Install Redis
apt install redis-server
# Configure Redis memory limit
# /etc/redis/redis.conf
maxmemory 256mb
maxmemory-policy allkeys-lru
systemctl restart redis-serverOPcache for PHP
# /etc/php/*/fpm/php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60Database Optimization
Index Optimization
# Find slow queries (MySQL)
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
# Analyze slow queries
mysqldumpslow /var/log/mysql/mysql-slow.log
# Add indexes to frequently queried columns
CREATE INDEX idx_user_email ON users(email);
CREATE INDEX idx_posts_created ON posts(created_at);Query Optimization Tips
- Use EXPLAIN to analyze queries
- Avoid SELECT * - specify needed columns
- Use LIMIT for pagination
- Cache frequent queries
CDN Integration
Offload static content to a CDN for better global performance:
Cloudflare
Free plan available, easy setup
BunnyCDN
Affordable, fast performance
KeyCDN
Pay-as-you-go pricing
Quick Wins Checklist
- ✓ Enable HTTP/2 and compression
- ✓ Add noatime to filesystem mounts
- ✓ Configure OPcache for PHP
- ✓ Set up basic caching (Redis/Memcached)
- ✓ Optimize database queries and add indexes
- ✓ Use a CDN for static assets
- ✓ Enable browser caching
- ✓ Compress images
- ✓ Minimize CSS/JS files
- ✓ Monitor with basic tools (htop, iotop)
Pro Tip
Start with easy optimizations that give the biggest impact. Measure before and after to see what actually helps. Not every optimization will benefit every application.
