5

we are moving our prototype service to production environment. I dont find any best library/way to do mysql read replication in golang sql library.

Our Mysql database services running on AWS RDS (one master and stwo slaves).

Need to do the following.

  1. insert/update/delete should go to master
  2. read from any database (including master also fine)

currently we have only one database connection.

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)


db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test")

How can we make our db instance to work with read replicas in golang ? Thanks.

Vish K
  • 135
  • 5
  • 14
  • There isn't a standard way to do this. At my last job, we had a custom-made db wrapper handle this. There were also some libraries that did similar things, but at the time at least, they all had serious drawbacks that prevented us from using them. – Jonathan Hall May 17 '18 at 17:54
  • you mean you have created a custom code that open multiple db connections (as on above code) and selects and return the db connection pointer based on our CRUD calls ? – Vish K May 17 '18 at 18:34
  • Yes, that's correct. In our case, it was also possible to specifically request reads from the master for certain operations, where the replication delay was unacceptable. – Jonathan Hall May 17 '18 at 18:38
  • For those still stuck on this, I made a library here https://github.com/bxcodec/dbresolver. We used this internally. And I think it's not bad to share it in public. – Iman Tumorang Dec 12 '22 at 03:33

0 Answers0