Setting the session pool size
YDB creates an actor for each session. Consequently, the session pool size on the client affects resource consumption (memory, CPU) on the YDB server.
For example, if 1000 clients of the same database open 1000 sessions each, one million actors are created on the server, consuming a large amount of memory and CPU. Without a client-side session limit, this can slow the cluster down and put it in a degraded state.
By default, native YDB SDK drivers use a limit of 50 sessions. When using third-party libraries such as Go database/sql, no limit is set.
Set the client session limit to the minimum needed for normal application operation. Remember that a session is single-threaded on both server and client. If the application must run 1000 concurrent in-flight requests to YDB, set the limit to about 1000 sessions.
Distinguish estimated RPS (requests per second) from in-flight requests. RPS is the total number of requests completed in one second. For example, with RPS = 10000 and average latency 100 ms, a limit of 1000 sessions is enough: each session can run about 10 sequential requests per second on average.
Below are examples of setting the session pool limit in different YDB SDKs.
package main
import (
"context"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
db, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithSessionPoolSizeLimit(500),
)
if err != nil {
panic(err)
}
defer db.Close(ctx)
...
}
The database/sql library has its own connection pool. Each database/sql connection maps to one YDB session. Tune the pool with sql.DB.SetMaxOpenConns and sql.DB.SetMaxIdleConns. See the database/sql documentation.
Example using a database/sql connection pool:
package main
import (
"context"
"database/sql"
_ "github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
db, err := sql.Open("ydb", os.Getenv("YDB_CONNECTION_STRING"))
if err != nil {
panic(err)
}
defer db.Close()
db.SetMaxOpenConns(100)
db.SetMaxIdleConns(100)
db.SetConnMaxIdleTime(time.Second) // workaround for background keep-aliving of YDB sessions
...
}
this.queryClient = QueryClient.newClient(transport)
// 10 — minimum active sessions kept in the pool during cleanup
// 500 — maximum pool size
.sessionPoolMinSize(10)
.sessionPoolMaxSize(500)
.build();
JDBC applications typically use external connection pools such as HikariCP or C3p0. By default, the YDB JDBC driver observes how many connections the external pool opens and adjusts the session pool accordingly. Configure session limits by configuring HikariCP or C3p0 correctly.
Example HikariCP settings in Spring:
spring.datasource.url=jdbc:ydb:grpc://localhost:2136/local
spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver
spring.datasource.hikari.maximum-pool-size=100 # max JDBC connections
import os
import ydb
with ydb.Driver(
connection_string=os.environ["YDB_CONNECTION_STRING"],
credentials=ydb.credentials_from_env_variables(),
) as driver:
driver.wait(timeout=5)
with ydb.QuerySessionPool(driver, size=500) as pool:
# ...
import os
import ydb
import asyncio
async def ydb_init():
async with ydb.aio.Driver(
connection_string=os.environ["YDB_CONNECTION_STRING"],
credentials=ydb.credentials_from_env_variables(),
) as driver:
await driver.wait()
async with ydb.aio.QuerySessionPool(driver, size=500) as pool:
# ...
asyncio.run(ydb_init())
Setting the pool size is not currently supported.
This section is under development.