Skip to content

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.

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, ...).

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, and applied_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.

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().

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.