Database and Redis
The database and Redis capabilities provide connections. Connections are the kind of thing that stays in configuration: a named instance holds the URL, credentials and pool settings. What you do with a connection (lock stores, rate limiters, push logs) is code that names the instance it uses.
Database instances
Section titled “Database instances”PostgreSQL is the only supported database system. Several named instances may be configured, but use cases, the outbox and migrations are limited to the default instance; other instances can be queried directly.
database: default: main instances: main: url: jdbc:postgresql://localhost:5432/app username: app password: "" # a secret: secrets.local.yaml or DATABASE_INSTANCES_MAIN_PASSWORD pool: { max: 10, min-idle: 1, connect-timeout: 5s, login-timeout: 10s } migrate: true| Key | Default | Range | Meaning |
|---|---|---|---|
url |
required | the connection URL. jdbc:postgresql:// is accepted by both languages |
|
username, password |
the password is a secret; both win over credentials in the URL (Go) | ||
pool.max |
10 | 1 to 1000 | pool size |
pool.min-idle |
1 | 0 to pool.max |
idle connections kept |
pool.connect-timeout |
5s | 250 ms to 5 min | how long a caller waits for a pooled connection |
pool.login-timeout |
10s | 1 s to 5 min, whole seconds | Java only. How long the driver may spend on one attempt (TCP, TLS, authentication), so a server that accepts the connection and never answers cannot hold a pool thread for ever |
migrate |
true | apply migrations at start-up; counts on the default instance |
Every range is checked at start-up; a value outside it is refused, never raised silently. There is no
driver key: a leftover one fails start-up.
Start-up opens a connection per instance and fails, naming the instance and the cause, if it cannot.
Each instance gets a critical health check database:<name>, and pools close at shutdown.
DatabaseModule provides Databases (the registry: get(name), defaultInstance(), names()) and a
Database per instance, each a HikariCP pool over the PostgreSQL JDBC driver. A Database exposes
name(), dataSource(), ping(), tableExists(qualifiedName), requireTable(qualifiedName, needer)
(for a capability that needs a table on a non-default instance) and migrates().
App.builder().module(new DatabaseModule())A start-up that fails on a timeout says so: cannot connect to database instance '<name>': ... (limits: pool.connect-timeout ..., pool.login-timeout ...; the database is not answering, ...).
database.Declare(app) registers the settings; database.Connect(ctx, b, declared) opens a pgx v5
pool (pgxpool) per instance, pings it, adds the check and closes the pool at shutdown. It returns
the instances; DB.Pool() is what sqlc-generated code takes, and DB.RequireTable is for a capability
that needs a table on a non-default instance.
url accepts jdbc:postgresql:// (the prefix is dropped), postgres:// and postgresql://. pgx’s
key=value form is refused, because the Java framework cannot read it either.
database.Tx is a transaction the framework began (the use-case runner, a receiver’s once).
Application code uses its Conn() inside a plan step and can never begin one: the constructor is
internal. database.Filter accumulates positional conditions for dynamic list queries; column names
must be constants.
Migrations
Section titled “Migrations”Migrations are plain SQL files, forward only, named V<version>__<description>.sql with a positive
integer version (for example V0001__create_orders.sql). Each module contributes at most one set (its
module id plus a location), and your application contributes its own the same way. Sets are applied to
the default database instance, in module registration order and by version within a set, only when
that instance’s migrate is true.
- Each file runs in its own transaction together with its row in
fw_schema_history; a failing file rolls back and fails the start. - A PostgreSQL advisory lock (the same one in Java and Go) makes instances starting together apply each file once. A Java and a Go replica can start against one database.
- The history row holds the module, version, description (
_read as a space), the SHA-256 of the file bytes, andapplied_at. - Go refuses a second set from one module; Java does not check, but the history is keyed by module and version, so a second set would collide. Contribute one set per module.
- A changed checksum of an applied file, an applied version whose file is gone, a file below the highest applied version, or two files with one version fails start-up.
Registration order is migration order: list the module whose tables you depend on (the outbox, locks) before your own.
MigrationSet.contribute(app, OrdersModule.class, "db/migration/orders");Call it from register. The class is used only for its class loader, so pass any class from the jar
that holds the SQL directory.
A package contributes a set with Declarer.Migrations(fs), usually an embedded directory
(dblock.Migrations() is the lock table’s). database.Migrate(ctx, b, dbs) applies every set.
database.ApplyMigrations runs the runner outside an application (tests, tools).
The Go runner writes exactly the rows the Java runner writes and accepts a database the Java runner
migrated, checked against conformance/migrations/.
redis: default: default instances: default: { url: "redis://localhost:6379/0", timeout: 2s }| Key | Default | Range | Meaning |
|---|---|---|---|
url |
required | redis:// or rediss:// (TLS); may carry a user, password and database number |
|
password |
a secret; wins over the URL’s | ||
timeout |
2s | more than 0, at most 5 min | command timeout |
pool.max |
16 | 1 to 1000 | pool size |
Redis is standalone only: no Cluster or Sentinel. Each instance has a critical health check
redis:<name> and closes at shutdown.
RedisModule provides RedisConnections (get(name), defaultInstance(), names()), each a
RedisConnection with name(), client() (a Jedis RedisClient) and ping().
redisfw.Declare(app) and redisfw.Connect(ctx, b, declared) make one go-redis v9 client per instance
and ping it. User info without a : in the URL is the password, as in Java. Client.Redis() is the
go-redis client.
Redis is optional. Only the Redis lock driver, the Redis push log and the Redis rate limiter need it.
Specification: Database and Redis. Where the instances sit in the configuration model: Configuration.