Gorm query takes >1min to commit

I’m having performance issues using a controller-service-repository architecture in my Golang app.

I’m using Postman to make a request, which takes over a minute to successfully insert a record in the database. I’ve also tried giving the controller direct access to the database, but the same issue occurs.

When I write a simple handler to establish a connection to the database and perform a query, the query executes quickly.

If anyone has an idea of what might be causing the performance issue, please let me know.

Here is the code for the main function, as well as the controller, service and repository:

func main() {
    r := setupRouter()

    pgInstance := db2.ConnectToDatabase()

    repo := repository.NewRepository(pgInstance)

    service := service2.NewService(repo)

    c := controller2.NewController(service)

    routes.InitializeRoutes(r, c)

    // Listen and Server in 0.0.0.0:8080
    r.Run(":8080")
}

Controller:

type Controller struct {
    service service.Service
}

func NewController(service service.Service) Controller {
    return Controller{
        service: service,
    }
}

Service:

type Service struct {
    repository repository.Repository
}

func NewService(repo repository.Repository) Service {
    return Service{
        repository: repo,
    }
}

Repository:

type Repository struct {
    db *gorm.DB
}

func NewRepository(db *gorm.DB) Repository {
    return Repository{
        db: db,
    }
}

func (r Repository) Db() *gorm.DB {
    return r.db
}

The performance issue you are facing might be caused by the overhead of the controller-service-repository architecture. Each layer adds additional function calls and data transformations, which can impact the overall performance.

To improve performance, you can consider the following steps:

  1. Check the database connection and ensure it is properly configured and optimized for performance.

  2. Analyze the queries being executed in the repository layer. Make sure they are optimized and use indexes where necessary. You can also enable query logging in the database to see if any slow queries are being executed.

  3. Consider reducing the number of layers in your architecture. If the controller, service, and repository layers are not providing significant benefits, you can directly access the database from the controller. However, be cautious about potential security and maintainability issues.

  4. Profile your code to identify any bottlenecks or performance hotspots. Use tools like pprof to analyze CPU and memory usage.

  5. Consider using connection pooling to reuse database connections instead of establishing a new connection for each request.

  6. Optimize your code by using efficient data structures and algorithms. Avoid unnecessary data transformations and processing.

  7. Consider implementing caching mechanisms to reduce the frequency of database queries.

Remember, improving performance is an iterative process. Profile your code, make changes, and measure the impact to find the most effective optimizations for your specific use case.