Golang & Concurrency (50 Qs)•Part 3: Go Microservices, HTTP, gRPC & Architecture (33 - 44)
Database Connection Pooling in Go: `SetMaxOpenConns` and `SetMaxIdleConns`.
senior LevelSystem DesignDatabases & SQLgRPC
In Simple Words:
sql.Open() does not open an active database connection immediately—it initializes an abstract connection pool:
SetMaxOpenConns(n): Caps maximum active connections to the database. Essential to prevent crushing PostgreSQL/MySQL with thousands of connections during traffic spikes.SetMaxIdleConns(n): Number of idle connections kept open in the pool for reuse.SetConnMaxLifetime(d): Maximum duration a connection can be reused before being retired (prevents stale connections caused by cloud load balancer timeouts).