Arsitektur Web High Traffic dengan MySQL & Nginx: Panduan Production-Ready

Arsitektur Web High Traffic dengan MySQL & Nginx: Panduan Production-Ready

Website atau aplikasi dengan traffic tinggi memerlukan arsitektur yang scalable, cepat, dan tahan banting. Kombinasi MySQL sebagai database dan Nginx sebagai web server/reverse proxy sering digunakan karena performa tinggi dan hemat resource.

Artikel ini membahas arsitektur high traffic, optimasi Nginx, MySQL tuning, caching, load balancing, dan best practices untuk production.


1. Prinsip Arsitektur High Traffic

  1. Separation of Concerns
    • Pisahkan web server (Nginx) dan database server (MySQL)
    • Backend application (Node.js / PHP) di layer tersendiri
  2. Horizontal Scaling
    • Tambah server web / backend sesuai traffic → lebih hemat daripada vertical scaling
  3. Caching Layer
    • Gunakan Nginx cache, Redis / Memcached, MySQL query cache
  4. Load Balancing
    • Nginx dapat membagi request ke beberapa backend application → mencegah bottleneck
  5. High Availability
    • MySQL Master-Slave atau Cluster → failover jika master down

2. Arsitektur Umum

            ┌────────────┐
            │   Client   │
            └─────┬──────┘
                  │ HTTPS/HTTP
            ┌─────▼──────┐
            │   Nginx    │  <- Reverse Proxy, SSL Termination, Caching
            └─────┬──────┘
       ┌─────────┴─────────┐
       │                   │
┌──────▼──────┐       ┌────▼──────┐
│ Backend App │       │ Backend App│  <- PHP-FPM / Node.js
└──────┬──────┘       └────┬──────┘
       │                   │
       └─────────┬─────────┘
                 │
             ┌───▼───┐
             │ MySQL │  <- Master-Slave / Cluster
             └───────┘
  • Nginx menangani request & caching → backend fokus ke business logic
  • Backend menulis ke MySQL Master, membaca dari Slave
  • Query berat dapat dialihkan ke cache (Redis / Memcached)

3. Optimasi Nginx untuk High Traffic

3.1 Worker & Connections

worker_processes auto;
worker_connections 10240;
keepalive_timeout 65;
  • Sesuaikan dengan CPU & jumlah concurrent connections

3.2 Caching

  • Static content:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}
  • Dynamic microcaching:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mycache:10m max_size=1g inactive=60m use_temp_path=off;

server {
    location / {
        proxy_pass http://backend;
        proxy_cache mycache;
        proxy_cache_valid 200 5s;
        proxy_cache_use_stale error timeout updating;
    }
}

3.3 Load Balancing

upstream backend {
    least_conn;
    server backend1.example.com;
    server backend2.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
  • Metode least_conn ideal untuk backend dengan performa berbeda
  • Nginx bisa handle SSL termination dan rate limiting

4. Optimasi MySQL untuk High Traffic

4.1 Konfigurasi MySQL

  • Buffer & cache tuning:
[mysqld]
innodb_buffer_pool_size = 2G       # 50-70% RAM server
innodb_log_file_size = 512M
query_cache_size = 64M
max_connections = 500
  • InnoDB → performa transaksi tinggi, row-level locking

4.2 Master-Slave Replication

  • Master → handle write
  • Slave(s) → handle read query
  • Meningkatkan read scalability & failover

4.3 Indexing & Query Optimization

  • Gunakan index untuk kolom yang sering dicari
  • Hindari SELECT * → spesifik kolom
  • Gunakan EXPLAIN untuk menganalisis query

4.4 Connection Pooling

  • Gunakan pool (PHP-FPM, Node.js, ProxySQL) → mengurangi overhead membuka koneksi MySQL
  • Contoh Node.js: mysql2 pool, max 50 connection

5. Caching & Queue Layer

  • Gunakan Redis / Memcached:
    • Session store
    • Query cache
    • API response cache
  • Gunakan queue system (RabbitMQ / Kafka) untuk job berat → tidak blok request user

6. Scaling & High Availability

6.1 Web & Backend

  • Horizontal scaling dengan Nginx load balancing
  • Auto-scaling server cloud jika traffic spike

6.2 Database

  • MySQL Master-Slave → read/write separation
  • Optional: MySQL Cluster / Galera → multi-master HA

6.3 CDN

  • Gunakan CDN untuk static assets → mengurangi load VPS dan latency global

7. Security Layer

  • SSL termination di Nginx → backend cukup HTTP
  • Rate limiting → mencegah abuse / brute-force
  • DDoS mitigation → limit connection & request
  • Firewall & fail2ban → blokir IP mencurigakan

8. Monitoring

KomponenTools
Nginxaccess.log, error.log, Nginx Amplify, Grafana
BackendPM2 (Node.js), PHP-FPM status, New Relic
MySQLslow query log, performance_schema, Percona Monitoring
Systemhtop, iostat, netstat, Prometheus + Grafana
  • Pantau latency, CPU, RAM, connections, cache hit ratio
  • Alert jika backend atau DB overload

9. Studi Kasus High Traffic

Startup e-commerce:

  • Traffic: 50k concurrent user / hari
  • Arsitektur:
    • Nginx reverse proxy + caching
    • Backend Node.js + PHP-FPM
    • MySQL Master-Slave
    • Redis untuk session & cache
    • CDN untuk gambar
  • Hasil:
    • Response time <200ms
    • Backend tetap ringan → hemat VPS cost
    • Scalable dengan tambah backend server

10. Best Practices

  1. Pisahkan web server, backend, database
  2. Gunakan Nginx caching & load balancing
  3. Optimasi MySQL buffer & query
  4. Gunakan Redis/Memcached → kurangi query berat ke MySQL
  5. Load balancing read query ke MySQL slave(s)
  6. Implementasi SSL & security layer di Nginx
  7. Monitoring real-time → deteksi bottleneck cepat
  8. Horizontal scaling → tambah server saat traffic spike
  9. Gunakan CDN untuk static content

Kesimpulan

Arsitektur web high traffic dengan MySQL & Nginx memungkinkan:

  • Scalable & resilient → handle ribuan hingga ratusan ribu concurrent user
  • Fast response → caching & query optimization
  • High availability → MySQL master-slave, Nginx failover
  • Cost efficient → Nginx hemat resource, CDN mengurangi bandwidth

Dengan strategi ini, website production bisa tahan traffic tinggi, cepat, dan aman, sambil tetap hemat biaya infrastruktur.