Skip to content

Supported API for additional database pools and sessions bound to them #5984

Open

Description

@t-kietzmann

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 through serverpod/protocol.dart) returns a PostgresPoolManager, which is @internal (adapters/postgres/postgres_pool_manager.dart:15).
  • To get a Database on that pool we call DatabaseConstructor.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 the db getter (serverpod/lib/src/server/session.dart:108, :271-277); db is meant to be the server's pool.
  • We also use pod.runtimeParametersBuilder, pod.databaseInterceptor and pod.serializationManager (public fields on Serverpod, 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 global ValueEncoder.set(poolManager.encoder) on every construction (database.dart:36-38), which is fine only while all pools share a dialect.
  • PostgresPoolManager calls the runtimeParametersBuilder again in every onOpen (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 a databases: 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 onOpen runs the builder for every new connection.
  • Make ValueEncoder per pool (or per Database) instead of global.
  • Promote shutdownTasks out of experimental, 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.0 and serverpod_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/Database construction). 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions