Optimasi MariaDB dengan Thread Pool untuk Banyak Koneksi dan RAM Terbatas

Optimasi MariaDB dengan Thread Pool untuk Banyak Koneksi dan RAM Terbatas

MariaDB sejak versi 10.x mendukung thread pool plugin, yang sangat efisien untuk menangani ribuan koneksi secara bersamaan tanpa menghabiskan RAM seperti model thread per connection default (one-thread-per-connection).

Dengan thread pool, satu thread MariaDB bisa menangani banyak koneksi secara bergantian, sehingga:

  • Tidak setiap koneksi membuat thread baru → hemat memory
  • Bisa melayani ribuan koneksi dengan RAM terbatas (misal 1–2GB)
  • Workload read-heavy dan write-heavy tetap stabil

1. Mengaktifkan Thread Pool

  1. Pastikan MariaDB mendukung thread poolSHOW PLUGINS; Cari thread_pool plugin. Jika belum aktif, aktifkan di file konfigurasi (my.cnf atau 50-server.cnf): [mysqld] plugin_load_add = thread_pool.so thread_pool_size = 16 thread_handling = pool-of-threadsPenjelasan:
    • thread_pool_size = 16 → jumlah thread fisik pool, bisa disesuaikan dengan CPU cores.
    • thread_handling = pool-of-threads → mode thread pool.
    • Tidak perlu khawatir banyak koneksi, karena satu thread pool bisa melayani puluhan hingga ratusan koneksi.

2. Konfigurasi RAM Minimal tapi Bisa Menampung Banyak Koneksi

a. Buffer Global

innodb_buffer_pool_size = 256M      # 25-30% RAM untuk server 1GB
key_buffer_size = 16M               # untuk MyISAM, minimal
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M

b. Buffer Per-Koneksi (dikurangi untuk RAM terbatas)

sort_buffer_size = 512K
join_buffer_size = 512K
read_buffer_size = 256K
read_rnd_buffer_size = 256K

Penting: ini per-koneksi, jadi semakin banyak koneksi, semakin hemat jika buffer kecil.


c. Thread Pool Setting Tambahan

thread_pool_oversubscribe = 2       # jumlah koneksi per thread fisik
thread_pool_max_threads = 64        # maksimal thread pool fisik
  • thread_pool_oversubscribe = 2 → satu thread pool melayani 2 koneksi aktif bersamaan. Bisa naik untuk workload ringan.
  • thread_pool_max_threads = 64 → batasi jumlah thread pool agar tidak makan RAM berlebihan.

d. Max Connections

Jika menggunakan thread pool, max_connections bisa ditingkatkan jauh melebihi jumlah thread pool:

max_connections = 500
  • Karena thread pool melayani banyak koneksi bergantian, RAM tidak akan langsung habis meski ada 500 koneksi.
  • Tanpa thread pool, 500 koneksi × buffer per-connection bisa langsung memblow-up RAM.

3. Contoh Konfigurasi MariaDB Real untuk Server 1GB RAM

[mysqld]
# Thread Pool
plugin_load_add = thread_pool.so
thread_handling = pool-of-threads
thread_pool_size = 16
thread_pool_oversubscribe = 2
thread_pool_max_threads = 64

# Connection & Limits
max_connections = 500
connect_timeout = 10

# InnoDB
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M

# Key Buffer
key_buffer_size = 16M

# Per-Connection Buffers
sort_buffer_size = 512K
join_buffer_size = 512K
read_buffer_size = 256K
read_rnd_buffer_size = 256K

# Query Cache (opsional)
query_cache_size = 16M
query_cache_type = 1

Dengan setting ini:

  • RAM 1GB cukup untuk handle 500 koneksi aktif, karena thread pool mengefisienkan penggunaan thread.
  • Query read-heavy bisa melayani banyak client tanpa crash.
  • Query write-heavy masih stabil karena InnoDB buffer pool cukup untuk transaksi normal.

4. Tips Tambahan

  1. Gunakan connection pooling di aplikasi (PHP, Java, Python) agar jumlah koneksi tetap efisien.
  2. Monitoring thread pool: SHOW STATUS LIKE 'Thread_pool%'; Pantau Thread_pool_threads dan Thread_pool_idle_threads.
  3. Kurangi buffer per-connection jika server sering mencapai limit RAM.
  4. Slow query log tetap aktif untuk optimasi query, karena banyak koneksi tidak berarti performa optimal tanpa query yang efisien.

5. Kesimpulan

Dengan thread pool MariaDB dan konfigurasi buffer yang tepat:

  • Server dengan RAM minimal 1GB bisa menampung ratusan koneksi bersamaan.
  • Menghemat memory dibandingkan one-thread-per-connection default.
  • Cocok untuk aplikasi web high traffic, SaaS, atau aplikasi multi-user di VPS murah.