• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

262
Views
How to programmatically detect auto failover on AWS mysql aurora?

Our stack is nodejs with MySQL we're using MySQL connections pooling our MySQL database is managed on AWS aurora . in case of auto failover the master DB is changed the hostname stays the same but the connections inside the pool stays connected to the wrong DB. The only why we found in order to reset the connection is to roll our servers.

this is a demonstration of a solution I think could solve this issue but I prefer a solution without the set interval

const mysql = require('mysql');


class MysqlAdapter {
    constructor() {
        this.connectionType = 'MASTER';
        this.waitingForAutoFaileOverSwitch = false;
        this.poolCluster = mysql.createPoolCluster();
        this.poolCluster.add(this.connectionType, {
            host: 'localhost',
            user: 'root',
            password: 'root',
            database: 'app'
        });

        this.intervalID = setInterval(() => {
            if(this.waitingForAutoFaileOverSwitch) return;
            this.excute('SHOW VARIABLES LIKE \'read_only\';').then(res => {
                // if MASTER is set to read only is on then its mean a fail over is accoure and swe need to switch all connection in poll to secondry database
                if (res[0].Value === 'ON') {
                    this.waitingForAutoFaileOverSwitch = true
                    this.poolCluster.end(() => {
                        this. waitingForAutoFaileOverSwitch = false
                    });
                };
            });
        }, 5000);

    }
    async excute(query) {
        // delay all incoming request until pool kill all connection to read only database
        if (this.waitingForAutoFaileOverSwitch) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    this.excute(query).then(res => {
                        resolve(res);
                    });
                }, 1000);
            });
        }
        return new Promise((resolve, reject) => {

            this.poolCluster.getConnection(this.connectionType, (err, connection) => {
                if (err) {
                    reject(err);
                }
                connection.query(query, (err, rows) => {
                    connection.release();
                    if (err) {
                        reject(err);
                    }
                    resolve(rows);
                });
            });
        });
    }
}



const adapter = new MysqlAdapter();

Is there any other programmable way to reset the connection inside the pool?

Is there any notification we can listing to In case of auto-failover?

over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

Instead of manually monitoring the DB health, as you have also hinted, ideally we subscribe to failover events published by AWS RDS Aurora.

There are multiple failover events listed here for the DB cluster: Amazon RDS event categories and event messages

You can use and test to see which one of them is the most reliable in your use case for triggering poolCluster.end() though.

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error