Performance Optimization
Complete guide to optimizing your VPS performance. Learn CPU tuning, memory optimization, disk I/O improvements, and caching strategies for maximum speed.
- Root or sudo access to your VPS
- Basic understanding of Linux system administration
- Knowledge of your application stack
- Monitoring tools installed (htop, iotop, etc.)
- Backup of current system configuration
Effective performance optimization starts with understanding your system's resource utilization. Regular monitoring helps identify bottlenecks and performance issues.
Key Performance Metrics:
- CPU Usage - Processor utilization and load averages
- Memory Usage - RAM consumption and swap activity
- Disk I/O - Storage read/write performance
- Network I/O - Bandwidth utilization and latency
- System Load - Overall system responsiveness
# Real-time system monitoring
top
htop
atop
# CPU performance analysis
mpstat 1
sar -u 1 5
iostat -c 1
# Memory analysis
free -h
vmstat 1
cat /proc/meminfo
# Disk I/O monitoring
iostat -x 1
iotop
fio --name=randread --rw=randread --bs=4k --size=256m --numjobs=4 --runtime=10
# Network monitoring
sar -n DEV 1
iftop
nload
# System load analysis
uptime
cat /proc/loadavg
wCPU optimization involves tuning processor scheduling, managing processes, and optimizing applications for better performance.
CPU Optimization Strategies:
- Process priority management
- CPU affinity settings
- Kernel scheduling parameters
- Background process optimization
- Multi-core utilization
# CPU performance tuning
# Check CPU information
lscpu
cat /proc/cpuinfo
# Process priority management
nice -n 10 command # Lower priority
chrt --rr 50 command # Real-time priority
# CPU affinity (pin processes to specific cores)
taskset -c 0-3 command # Use cores 0-3
taskset -p 1234 # Check process affinity
# Kernel parameters for CPU performance
sudo sysctl -w kernel.sched_min_granularity_ns=10000000
sudo sysctl -w kernel.sched_wakeup_granularity_ns=15000000
# CPU frequency scaling
cpupower frequency-info
cpupower frequency-set -g performance
# Optimize for compute-intensive workloads
echo 'vm.dirty_ratio = 10' | sudo tee -a /etc/sysctl.conf
echo 'vm.dirty_background_ratio = 5' | sudo tee -a /etc/sysctl.conf
# Apply changes
sudo sysctl -pMemory optimization focuses on efficient RAM usage, reducing swap activity, and implementing caching strategies.
Memory Optimization Techniques:
- Memory usage monitoring
- Swap optimization
- Cache management
- Memory allocation tuning
- Application-specific optimization
# Memory optimization settings
# Check current memory usage
free -h
cat /proc/meminfo
# Optimize swap usage
sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=50
# Memory overcommit settings
sudo sysctl -w vm.overcommit_memory=1
sudo sysctl -w vm.overcommit_ratio=50
# Huge pages for better memory performance
echo 'vm.nr_hugepages = 128' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Memory defragmentation
echo 'vm.compact_memory = 1' | sudo tee /proc/sys/vm/compact_memory
# Application-specific memory tuning
# For databases
echo 'vm.dirty_writeback_centisecs = 1500' | sudo tee -a /etc/sysctl.conf
# For web servers
echo 'net.core.somaxconn = 65535' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_max_syn_backlog = 65535' | sudo tee -a /etc/sysctl.conf
# Apply all changes
sudo sysctl -p
# Monitor memory performance
vmstat 1
sar -r 1Disk I/O optimization improves file system performance, reduces latency, and enhances overall system responsiveness.
Disk Optimization Methods:
- File system tuning
- I/O scheduler optimization
- RAID configuration
- SSD optimization
- Storage caching
# Disk I/O optimization
# Check disk performance
hdparm -t /dev/sda
dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync
# I/O scheduler tuning
cat /sys/block/sda/queue/scheduler
echo 'deadline' | sudo tee /sys/block/sda/queue/scheduler
# File system optimization
sudo tune2fs -l /dev/sda1
# ext4 optimization
sudo tune2fs -o journal_data_writeback /dev/sda1
sudo tune2fs -O ^has_journal /dev/sda1
# SSD optimization
sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer
# Disable access time updates
sudo mount -o remount,noatime,nodiratime /
# Add to fstab for persistence
echo 'UUID=your-uuid / ext4 noatime,nodiratime,errors=remount-ro 0 1' | sudo tee -a /etc/fstab
# RAID optimization (if applicable)
sudo mdadm --detail /dev/md0
sudo blockdev --setra 1024 /dev/md0
# Storage caching
sudo apt install bc -y
echo 'vm.dirty_ratio = 15' | sudo tee -a /etc/sysctl.conf
echo 'vm.dirty_background_ratio = 10' | sudo tee -a /etc/sysctl.confNetwork optimization improves connection speed, reduces latency, and enhances overall network performance.
Network Performance Areas:
- TCP stack tuning
- Connection optimization
- Buffer size configuration
- Network interface tuning
- Quality of Service (QoS)
# Network performance tuning
# TCP optimization
sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216
sudo sysctl -w net.ipv4.tcp_rmem='4096 87380 16777216'
sudo sysctl -w net.ipv4.tcp_wmem='4096 65536 16777216'
# Connection optimization
sudo sysctl -w net.core.somaxconn=65535
sudo sysctl -w net.core.netdev_max_backlog=5000
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=1024
# Time-wait optimization
sudo sysctl -w net.ipv4.tcp_tw_reuse=1
sudo sysctl -w net.ipv4.tcp_tw_recycle=0
sudo sysctl -w net.ipv4.tcp_fin_timeout=30
# Network interface tuning
sudo ethtool -s eth0 speed 1000 duplex full autoneg off
sudo ethtool -K eth0 tso off gso off
# Check network performance
ping -c 10 google.com
iperf3 -c speedtest.net
# Monitor network connections
ss -tlnp
netstat -tlnp
# DNS optimization
echo 'nameserver 8.8.8.8' | sudo tee /etc/resolv.conf
echo 'nameserver 8.8.4.4' | sudo tee -a /etc/resolv.conf
# Apply network changes
sudo sysctl -pApplication-specific optimization involves tuning web servers, databases, and other services for optimal performance.
Application Optimization:
- Web server tuning
- Database optimization
- PHP/Python optimization
- Cache implementation
- Application profiling
# Web server optimization (Apache/Nginx)
# Apache optimization
sudo nano /etc/apache2/mods-available/mpm_prefork.conf
# Increase MaxRequestWorkers
# MaxRequestWorkers 256
# Nginx optimization
sudo nano /etc/nginx/nginx.conf
# worker_processes auto;
# worker_connections 1024;
# use epoll;
# multi_accept on;
# PHP optimization
sudo nano /etc/php/8.1/fpm/php.ini
# memory_limit = 256M
# max_execution_time = 300
# upload_max_filesize = 100M
# post_max_size = 100M
# Database optimization (MySQL)
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# innodb_buffer_pool_size = 1G
# innodb_log_file_size = 256M
# max_connections = 100
# query_cache_size = 128M
# tmp_table_size = 128M
# Redis optimization
sudo nano /etc/redis/redis.conf
# maxmemory 256mb
# maxmemory-policy allkeys-lru
# tcp-keepalive 60
# Restart services after optimization
sudo systemctl restart apache2
sudo systemctl restart nginx
sudo systemctl restart mysql
sudo systemctl restart redis-serverImplementing effective caching strategies can dramatically improve application performance and reduce server load.
Caching Solutions:
- Page caching
- Object caching
- Database query caching
- CDN integration
- Application-level caching
# Install caching solutions
sudo apt install redis-server memcached varnish -y
# Redis configuration
sudo nano /etc/redis/redis.conf
# maxmemory 256mb
# maxmemory-policy allkeys-lru
# bind 127.0.0.1
# protected-mode yes
# Memcached configuration
sudo nano /etc/memcached.conf
# -m 256
# -p 11211
# -u memcache
# -l 127.0.0.1
# Varnish configuration
sudo nano /etc/varnish/default.vcl
# backend default {
# .host = "127.0.0.1";
# .port = "8080";
# }
# PHP OPcache
sudo nano /etc/php/8.1/fpm/php.ini
# opcache.enable=1
# opcache.memory_consumption=256
# opcache.max_accelerated_files=7963
# opcache.revalidate_freq=0
# WordPress caching (if applicable)
sudo apt install php8.1-redis -y
# Add to wp-config.php
# define('WP_CACHE', true);
# define('WP_REDIS_HOST', '127.0.0.1');
# Start caching services
sudo systemctl enable redis-server
sudo systemctl enable memcached
sudo systemctl enable varnish
sudo systemctl start redis-server
sudo systemctl start memcached
sudo systemctl start varnish
# Test caching
redis-cli ping
echo "stats settings" | nc localhost 11211Continuous performance monitoring and regular benchmarking help maintain optimal system performance and identify degradation.
Monitoring & Benchmarking Tools:
- System monitoring tools
- Performance benchmarking
- Load testing
- Application profiling
- Automated monitoring
# Install benchmarking tools
sudo apt install sysbench stress-ng -y
# CPU benchmarking
sysbench cpu --cpu-max-prime=20000 run
stress-ng --cpu 4 --timeout 60s
# Memory benchmarking
sysbench memory --memory-block-size=1K --memory-total-size=10G run
stress-ng --vm 2 --vm-bytes 1G --timeout 60s
# Disk benchmarking
sysbench fileio --file-total-size=1G prepare
sysbench fileio --file-total-size=1G --file-test-mode=rndrw run
sysbench fileio --file-total-size=1G cleanup
# Network benchmarking
iperf3 -s # Server mode
iperf3 -c localhost # Client mode
# Application benchmarking
ab -n 1000 -c 10 http://localhost/
siege -c 100 -t 60S http://localhost/
# Continuous monitoring setup
# Install monitoring tools
sudo apt install prometheus-node-exporter -y
sudo systemctl enable prometheus-node-exporter
sudo systemctl start prometheus-node-exporter
# Monitor with htop (persistent)
htop
# Create performance monitoring script
sudo nano /usr/local/bin/performance_monitor.sh
#!/bin/bash
LOG_FILE="/var/log/performance_monitor.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Get system metrics
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *([0-9.]*)%* id.*/\1/" | awk '{print 100 - $1}')
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.2f", $3/$2 * 100.0}')
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
# Log metrics
echo "$TIMESTAMP - CPU: ${CPU_USAGE}% Memory: ${MEMORY_USAGE}% Disk: ${DISK_USAGE}%" >> $LOG_FILE
# Alert if high usage
if (( $(echo "$CPU_USAGE > 90" | bc -l) )); then
echo "High CPU usage detected: ${CPU_USAGE}%" | mail -s "CPU Alert" admin@example.com
fi
# Clean old logs (keep last 30 days)
find /var/log -name "*performance*" -mtime +30 -deleteCan't find what you're looking for? Our support team is here to help.
