TypeORM not recognizing new Aurora MySQL read replicas after autoscaling

Hey everyone, I’m having a weird issue with TypeORM and Aurora MySQL. We’re using autoscaling for our read replicas, but TypeORM doesn’t seem to pick up new ones when they’re added.

Our setup:

  • TypeORM 0.3.20
  • NestJS TypeORM 10.0.2
  • Node.js v22.13
  • Aurora MySQL 8.0 (autoscaling read replicas)

We’ve got separate endpoints for read and write operations. The problem is, when Aurora adds a new read replica, TypeORM keeps sending all read traffic to the original instance. It’s like it’s not re-checking the endpoint or updating its connection pool.

Here’s a simplified version of our TypeORM config:

TypeOrmModule.forRoot({
  type: 'mysql',
  replication: {
    master: { /* master config */ },
    slaves: [{ /* read replica config */ }],
  },
  // other settings
});

What I’ve noticed:

  1. TypeORM only sets up its replication pool when the app starts
  2. It doesn’t seem to check the DNS of the read endpoint again
  3. New replicas aren’t used until we restart the app

Is there a way to make TypeORM:

  1. Refresh its connection pool while running?
  2. Re-check the read endpoint DNS without a restart?
  3. Handle it smoothly if a replica disappears?

I’m pretty sure our AWS setup is correct. Also, I found out Node.js might cache DNS lookups for up to an hour by default. Could this be part of the problem?

Any ideas on how to fix this? Thanks!

I’ve encountered this type of issue before in production. The core of the problem seems to be that TypeORM initializes its connection pool on startup and does not reevaluate the available replicas during runtime. In my case, I built a custom connection manager that periodically verifies the Aurora endpoints. It checks AWS for any new replica endpoints and refreshes the connection pool accordingly. Adjusting Node.js DNS TTL settings to force more frequent lookups also helped mitigate the problem. This approach allowed us to better handle dynamic scaling without needing a full application restart.