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

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. }).Register(dbresolver.Config{
  14. // use `db1` as sources (DB's default connection), `db5` as replicas for `User`, `Address`
  15. Replicas: []gorm.Dialector{mysql.Open("db5_dsn")},
  16. }, &User{}, &Address{}).Register(dbresolver.Config{
  17. // use `db6`, `db7` as sources, `db8` as replicas for `orders`, `Product`
  18. Sources: []gorm.Dialector{mysql.Open("db6_dsn"), mysql.Open("db7_dsn")},
  19. Replicas: []gorm.Dialector{mysql.Open("db8_dsn")},
  20. }, "orders", &Product{}, "secondary"))

Transaction

When using transaction, DBResolver will use the transaction and won’t switch to sources/replicas

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, 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 using GORM callback.

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)

Load Balancing

GORM supports load balancing sources/replicas based on policy, the policy is an interface 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 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. )