Making Super Fast MySQL Database with RAM Disk and Master-Master Replication for Data Integrity
I am planning to create ram disk and use it as for mysql server will have all mysql files in it so read write happens in ram disk.
Also to maintain persistence I will do remote mysql master master backup.
Is it a good idea ?
Will there be performance issue can I get ram read write in main mysql server ?
How would you approach this to get database speed really high and maintain backup so no data loss
ChatGPT:
Your idea of using a RAM disk for MySQL combined with master-master replication for persistence can deliver blazing fast performance, but there are key considerations to ensure stability, reliability, and long-term success. Here’s how I’d approach it:
1. Setting Up the RAM Disk for MySQL
Using a RAM disk can significantly boost MySQL’s read/write performance since RAM offers ultra-fast I/O speeds compared to SSDs or HDDs.
Steps for RAM Disk Setup (Ubuntu 22.04):
mkdir /mnt/ramdisk
mount -t tmpfs -o size=4G tmpfs /mnt/ramdisk
- Adjust
size=
based on available RAM. - Add this to
/etc/fstab
for automatic mounting on reboot:
tmpfs /mnt/ramdisk tmpfs rw,size=4G 0 0
Move MySQL Data Directory to RAM Disk:
- Stop MySQL:
systemctl stop mysql
- Copy data to RAM disk:
cp -R /var/lib/mysql/* /mnt/ramdisk/
- Update MySQL config:
In/etc/mysql/mysql.conf.d/mysqld.cnf
datadir = /mnt/ramdisk
- Start MySQL:
systemctl start mysql
2. Master-Master Replication for Persistence
Since RAM is volatile, replication is crucial to prevent data loss.
- Server A (Primary RAM Disk) → Server B (Backup with Persistent Storage)
- Use semi-synchronous replication for improved data integrity.
Steps for Master-Master Setup:
- On both servers, add in
mysqld.cnf
:
server-id = 1 # Change to 2 on the second server
log_bin = mysql-bin
binlog_do_db = your_database - On Server A (RAM Disk), create a replication user:
CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
- On Server B, run:
CHANGE MASTER TO MASTER_HOST='Server_A_IP', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 154; START SLAVE;
- Repeat the process for Server A to point back to Server B.
3. Recommended Optimizations for Maximum Speed
✅ Increase InnoDB Buffer Pool Size (to fit most of your data in memory)
innodb_buffer_pool_size = 2G
✅ Tweak tmpfs
Mount Options
- Use
noatime
to reduce write overhead.
mount -t tmpfs -o rw,noatime,size=4G tmpfs /mnt/ramdisk
✅ Tune MySQL Settings for Performance
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 0
sync_binlog = 0
✅ Reduce Disk Writes
- Set
innodb_flush_log_at_trx_commit = 2
(less durable but faster).
✅ Optimize Table Indexing
- Ensure key fields have proper indexing for faster lookups.
4. Backup Strategy
To ensure no data loss:
- Use
mysqldump
orxtrabackup
to sync data to persistent storage at regular intervals. - Automate backups every 5-10 minutes using
cron
:
*/5 * * * * /usr/bin/mysqldump -u root -p'your_password' your_database | gzip > /backup/mysql_backup_$(date +\%F_\%T).sql.gz
- Store backups on both the local persistent disk and a remote server for redundancy.
5. Potential Risks & Solutions
✅ Power Loss Risk: RAM is volatile. Ensure replication + frequent dumps.
✅ Memory Pressure Risk: If RAM is overloaded, MySQL performance may suffer. Monitor with htop
or free -h
.
✅ Crash Recovery Risk: Enable binlogs and point-in-time recovery.
Ideal Setup for Peak Performance and Safety
✅ RAM Disk for MySQL data
✅ Master-Master replication for redundancy
✅ Automated backups every 5-10 minutes
✅ Tweaked MySQL settings for faster memory utilization
This combination should give you lightning-fast performance while minimizing data loss risk.
Why use MySQL over CSV which one gives Faster Read/Write Performance? - rubi.me
[…] Tip: How you can setup Super Fast MySQL Database with RAM Disk […]