Repository navigation
Description
Problem to Solve
We run background services (an event sequencer, change feed readers, snapshot readers) on their own connection pools, so that a burst of RPCs that exhausts the main pool cannot starve them. This works in 4.1.0, but every building block is internal or experimental:
DatabaseProvider.forDialect(dialect).createPoolManager(...)(serverpod_database/lib/src/interface/provider.dart:27, exported throughserverpod/protocol.dart) returns aPostgresPoolManager, which is@internal(adapters/postgres/postgres_pool_manager.dart:15).- To get a
Databaseon that pool we callDatabaseConstructor.create(session:, poolManager:)(serverpod_database/lib/src/database.dart:6-19), documented as "Extension to only expose the [Database] constructor internally within the package", although it is exported (serverpod_database.dart:19). - To give generated ORM code a session on that pool we subclass
InternalSession(server:, enableLogging: false)and override thedbgetter (serverpod/lib/src/server/session.dart:108,:271-277);dbis meant to be the server's pool. - We also use
pod.runtimeParametersBuilder,pod.databaseInterceptorandpod.serializationManager(public fields onServerpod,serverpod.dart:438,:449). - To stop the pools on shutdown we use
pod.experimental.shutdownTasks.addTask/removeTask(serverpod/lib/src/server/serverpod.dart:1601-1631), which "may change or be removed between minor version releases".
Two more details we depend on:
Database._sets the globalValueEncoder.set(poolManager.encoder)on every construction (database.dart:36-38), which is fine only while all pools share a dialect.PostgresPoolManagercalls theruntimeParametersBuilderagain in everyonOpen(postgres_pool_manager.dart:74-94), which we use to validate each new physical connection. That is behaviour, not a documented promise.
Possible only through internal and experimental APIs, so every minor upgrade needs a dedicated test on our side.
Proposal
A supported way to declare additional pools and open sessions on them, for example:
final pool = pod.createDatabasePool(
name: 'background',
maxConnectionCount: 16,
// optional overrides: connection timeout, runtime parameters
);
final session = await pod.createSession(pool: pool, enableLogging: false);
// generated ORM code runs unchanged on session.db- Public
Serverpod.createDatabasePool(...)(or adatabases:section in the config) returning a handle;Serverpod.createSession(pool:). - Serverpod starts and stops these pools with the server (no experimental shutdown hook needed), applies the same runtime parameters builder and database interceptor, and documents that
onOpenruns the builder for every new connection. - Make
ValueEncoderper pool (or perDatabase) instead of global. - Promote
shutdownTasksout ofexperimental, or let pools register themselves.
Use Case
Next to the RPC pool we run two more pools: a small one (2 connections) for an event sequencer and one with 16 connections for change feed readers, a fan-out hub, snapshot readers, expiry jobs and permission re-reads. An RPC overload must not starve these services.
Alternatives
What we do today: our own pool manager created via DatabaseProvider.forDialect(...).createPoolManager(...) with its own PostgresDatabaseConfig (own maxConnectionCount and application_name), a Database via DatabaseConstructor.create with the server's interceptor, a session class that extends InternalSession and overrides db, and shutdown through pod.experimental.shutdownTasks. It works, but it rests on internal and experimental APIs, so we keep an upgrade test that covers the internal constructor, the global encoder and the onOpen behaviour.
Additional context
- Checked against
serverpod 4.1.0andserverpod_database 4.1.0; unchanged in 4.1.1. - Related: #5046 (open; a public API to obtain a database connection from config, motivated by the CLI migration runner and the circular
Session/Databaseconstruction). We found no issue about a second pool inside a running server.
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity