Skip to content

Commit 02714ea

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
feat(relay): make Postgres pool size configurable, default 50 (#3191)
## Summary - Raise the relay's Postgres pool cap from the `buzz-db` default of 20 to 50 per pool, and expose `BUZZ_DB_POOL_SIZE` for per-deploy tuning - Applies to the writer pool and, when `READ_DATABASE_URL` is set, the reader pool; zero/unparsable values fall back to the default - The `buzz-db` library default is unchanged — only the relay opts into the larger cap ## Why During the 2026-07-27 18:40–19:05Z traffic burst on bb-public, per-pod PG pools pinned at 20 fleet-wide and ~380 requests failed on the 3s acquire timeout — membership checks, channel access lookups, and historical queries returning errors to users. The database was nowhere near a limit: Aurora (db.r8g.8xlarge, ~5,000 max connections) sat at 19% CPU, 201 connections (~4% of capacity), commit latency flat at 0.01ms. The 20-connection default was sized for "four relay pods against PG max_connections=100" (the comment in `buzz-db` says exactly that). Production now runs 12–15 pods against Aurora — the per-pod cap is the binding constraint, not the DB. Budget at the new default: 15 pods × (50 writer + 50 reader + 5 audit) ≈ 1,575 potential connections, ~30% of Aurora's ceiling — and actual usage stays demand-driven (`min_connections` stays 2, connections only open under load). Same shape as #2521 (`BUZZ_REDIS_POOL_SIZE`), which fixed the identical class of ceiling on the Redis side. ## Testing - `cargo test -p buzz-relay`: 762 passed, 1 failed — the lone red is `api::mesh_demo::tests::demo_join_forwarded_arm_round_trips_echo`, the known pre-existing flake; it fails identically on clean `main` at the same SHA (verified via `git stash` / rerun) - New test `db_pool_size_env_override_and_invalid_fallback` covers override, zero, and unparsable fallback - `defaults_are_valid` extended to pin the new default - `cargo clippy -p buzz-relay --all-targets -- -D warnings` and `cargo fmt --check` clean Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Co-authored-by: Tyler Longwell <[email protected]>
1 parent 0cefffb commit 02714ea

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ REDIS_URL=redis://localhost:6379
3434
# Max connections in the relay's shared Redis pool (default 16).
3535
# BUZZ_REDIS_POOL_SIZE=16
3636

37+
# Max connections in each of the relay's Postgres pools — writer and, when
38+
# READ_DATABASE_URL is set, reader (default 50).
39+
# BUZZ_DB_POOL_SIZE=50
40+
3741
# -----------------------------------------------------------------------------
3842
# Typesense (search)
3943
# -----------------------------------------------------------------------------

‎crates/buzz-relay/src/config.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ pub struct Config {
6464
/// pod is only 4 — small enough that rate-limit checks, presence, and
6565
/// pub/sub publishes queue behind each other under load.
6666
pub redis_pool_size: usize,
67+
/// Maximum connections in the Postgres writer/reader pools. Defaults to 50.
68+
///
69+
/// The `buzz-db` default of 20 was sized for a handful of pods against
70+
/// `max_connections=100`. Against Aurora (~5,000 connections) that cap
71+
/// is the binding constraint: a burst of concurrent handlers exhausts
72+
/// the per-pod pool and requests fail on acquire timeout while the
73+
/// database sits idle.
74+
pub db_pool_size: u32,
6775
/// Public WebSocket URL of this relay, advertised in NIP-11.
6876
pub relay_url: String,
6977
/// Public WebSocket URL of the dedicated device-pairing relay, when configured.
@@ -424,6 +432,12 @@ impl Config {
424432
.filter(|&v| v > 0)
425433
.unwrap_or(16);
426434

435+
let db_pool_size = std::env::var("BUZZ_DB_POOL_SIZE")
436+
.ok()
437+
.and_then(|v| v.parse::<u32>().ok())
438+
.filter(|&v| v > 0)
439+
.unwrap_or(50);
440+
427441
let relay_url =
428442
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".to_string());
429443

@@ -875,6 +889,7 @@ impl Config {
875889
read_database_url,
876890
redis_url,
877891
redis_pool_size,
892+
db_pool_size,
878893
relay_url,
879894
pairing_relay_url,
880895
max_connections,
@@ -942,6 +957,7 @@ mod tests {
942957
assert!(!config.database_url.is_empty());
943958
assert!(!config.redis_url.is_empty());
944959
assert_eq!(config.redis_pool_size, 16);
960+
assert_eq!(config.db_pool_size, 50);
945961
assert!(config.max_connections > 0);
946962
assert!(config.send_buffer_size > 0);
947963
assert_eq!(config.max_frame_bytes, DEFAULT_MAX_FRAME_BYTES);
@@ -1009,6 +1025,31 @@ mod tests {
10091025
assert_eq!(junk, 16, "unparsable value must fall back to the default");
10101026
}
10111027

1028+
#[test]
1029+
fn db_pool_size_env_override_and_invalid_fallback() {
1030+
let _guard = ENV_MUTEX.lock().unwrap();
1031+
let previous = std::env::var_os("BUZZ_DB_POOL_SIZE");
1032+
1033+
std::env::set_var("BUZZ_DB_POOL_SIZE", "80");
1034+
let overridden = Config::from_env().expect("config").db_pool_size;
1035+
1036+
std::env::set_var("BUZZ_DB_POOL_SIZE", "0");
1037+
let zero = Config::from_env().expect("config").db_pool_size;
1038+
1039+
std::env::set_var("BUZZ_DB_POOL_SIZE", "not-a-number");
1040+
let junk = Config::from_env().expect("config").db_pool_size;
1041+
1042+
if let Some(value) = previous {
1043+
std::env::set_var("BUZZ_DB_POOL_SIZE", value);
1044+
} else {
1045+
std::env::remove_var("BUZZ_DB_POOL_SIZE");
1046+
}
1047+
1048+
assert_eq!(overridden, 80);
1049+
assert_eq!(zero, 50, "zero must fall back to the default");
1050+
assert_eq!(junk, 50, "unparsable value must fall back to the default");
1051+
}
1052+
10121053
#[test]
10131054
fn read_database_url_unset_or_blank_is_none() {
10141055
let _guard = ENV_MUTEX.lock().unwrap();

‎crates/buzz-relay/src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ async fn main() -> anyhow::Result<()> {
158158
let db_config = DbConfig {
159159
database_url: config.database_url.clone(),
160160
read_database_url: config.read_database_url.clone(),
161+
max_connections: config.db_pool_size,
161162
..DbConfig::default()
162163
};
163164
let db = Db::new(&db_config).await.map_err(|e| {

0 commit comments

Comments
 (0)