DBResolver adds multiple databases support to GORM, the following features are supported:

  • Multiple sources, replicas
  • Read/Write Splitting
  • Automatic connection switching based on the working table/struct
  • Manual connection switching
  • Sources/Replicas load balancing
  • Works for RAW SQL
  • Transaction

https://github.com/go-gorm/dbresolver

Usage

  1. import (
  2. "gorm.io/gorm"
  3. "gorm.io/plugin/dbresolver"
  4. "gorm.io/driver/mysql"
  5. )
  6. db, err := gorm.Open(mysql.Open("db1_dsn"), &gorm.Config{})
  7. db.Use(dbresolver.Register(dbresolver.Config{
  8. // use `db2` as sources, `db3`, `db4` as replicas
  9. Sources: []gorm.Dialector{mysql.Open("db2_dsn")},
  10. Replicas: []gorm.Dialector{mysql.Open("db3_dsn"), mysql.Open("db4_dsn")},
  11. // sources/replicas load balancing policy
  12. Policy: dbresolver.RandomPolicy{},
  13. // print sources/replicas mode in logger
  14. ResolverModeReplica: true,
  15. }).Register(dbresolver.Config{
  16. // use `db1` as sources (DB's default connection), `db5` as replicas for `User`, `Address`
  17. Replicas: []gorm.Dialector{mysql.Open("db5_dsn")},
  18. }, &User{}, &Address{}).Register(dbresolver.Config{
  19. // use `db6`, `db7` as sources, `db8` as replicas for `orders`, `Product`
  20. Sources: []gorm.Dialector{mysql.Open("db6_dsn"), mysql.Open("db7_dsn")},
  21. Replicas: []gorm.Dialector{mysql.Open("db8_dsn")},
  22. }, "orders", &Product{}, "secondary"))

Automatic connection switching

DBResolver will automatically switch connection based on the working table/struct

For RAW SQL, DBResolver will extract the table name from the SQL to match the resolver, and will use sources unless the SQL begins with SELECT (excepts SELECT... FOR UPDATE), for example:

  1. // `User` Resolver Examples
  2. db.Table("users").Rows() // replicas `db5`
  3. db.Model(&User{}).Find(&AdvancedUser{}) // replicas `db5`
  4. db.Exec("update users set name = ?", "jinzhu") // sources `db1`
  5. db.Raw("select name from users").Row().Scan(&name) // replicas `db5`
  6. db.Create(&user) // sources `db1`
  7. db.Delete(&User{}, "name = ?", "jinzhu") // sources `db1`
  8. db.Table("users").Update("name", "jinzhu") // sources `db1`
  9. // Global Resolver Examples
  10. db.Find(&Pet{}) // replicas `db3`/`db4`
  11. db.Save(&Pet{}) // sources `db2`
  12. // Orders Resolver Examples
  13. db.Find(&Order{}) // replicas `db8`
  14. db.Table("orders").Find(&Report{}) // replicas `db8`

Read/Write Splitting

Read/Write splitting with DBResolver based on the current used GORM callbacks.

For Query, Row callback, will use replicas unless Write mode specified
For Raw callback, statements are considered read-only and will use replicas if the SQL starts with SELECT

Manual connection switching

  1. // Use Write Mode: read user from sources `db1`
  2. db.Clauses(dbresolver.Write).First(&user)
  3. // Specify Resolver: read user from `secondary`'s replicas: db8
  4. db.Clauses(dbresolver.Use("secondary")).First(&user)
  5. // Specify Resolver and Write Mode: read user from `secondary`'s sources: db6 or db7
  6. db.Clauses(dbresolver.Use("secondary"), dbresolver.Write).First(&user)

Transaction

When using transaction, DBResolver will keep using the transaction and won’t switch to sources/replicas based on configuration

But you can specifies which DB to use before starting a transaction, for example:

  1. // Start transaction based on default replicas db
  2. tx := DB.Clauses(dbresolver.Read).Begin()
  3. // Start transaction based on default sources db
  4. tx := DB.Clauses(dbresolver.Write).Begin()
  5. // Start transaction based on `secondary`'s sources
  6. tx := DB.Clauses(dbresolver.Use("secondary"), dbresolver.Write).Begin()

Load Balancing

GORM supports load balancing sources/replicas based on policy, the policy should be a struct implements following interface:

  1. type Policy interface {
  2. Resolve([]gorm.ConnPool) gorm.ConnPool
  3. }

Currently only the RandomPolicy implemented and it is the default option if no other policy specified.

Connection Pool

  1. db.Use(
  2. dbresolver.Register(dbresolver.Config{ /* xxx */ }).
  3. SetConnMaxIdleTime(time.Hour).
  4. SetConnMaxLifetime(24 * time.Hour).
  5. SetMaxIdleConns(100).
  6. SetMaxOpenConns(200)
  7. )