Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(db): recover from Aurora/RDS failover hangs via tcp_user_timeout (#13629)

Sai Harsha Vardhanaecb1d14a527
diff --git a/config/config.example.toml b/config/config.example.toml
index 946667e2fb..9d6c23f0fb 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -58,6 +58,10 @@ connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+keepalives_idle = 5 # Idle seconds before the OS sends TCP keepalive probes
+keepalives_interval = 2 # Seconds between keepalive probes
+keepalives_count = 3 # Unanswered probes before the OS declares the connection dead
+tcp_user_timeout_ms = 5000 # Unacked-data timeout (ms); keep below connection_timeout
# Replica SQL data store credentials
[replica_database]
@@ -72,6 +76,10 @@ connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+keepalives_idle = 5 # Idle seconds before the OS sends TCP keepalive probes
+keepalives_interval = 2 # Seconds between keepalive probes
+keepalives_count = 3 # Unanswered probes before the OS declares the connection dead
+tcp_user_timeout_ms = 5000 # Unacked-data timeout (ms); keep below connection_timeout
# Accounts schema SQL data store credentials (merchant accounts, profiles, MCAs, key stores)
[accounts_database]
@@ -86,6 +94,10 @@ connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+keepalives_idle = 5 # Idle seconds before the OS sends TCP keepalive probes
+keepalives_interval = 2 # Seconds between keepalive probes
+keepalives_count = 3 # Unanswered probes before the OS declares the connection dead
+tcp_user_timeout_ms = 5000 # Unacked-data timeout (ms); keep below connection_timeout
# Global tenant (users, roles) SQL data store credentials
[global_database]
@@ -100,6 +112,10 @@ connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+keepalives_idle = 5 # Idle seconds before the OS sends TCP keepalive probes
+keepalives_interval = 2 # Seconds between keepalive probes
+keepalives_count = 3 # Unanswered probes before the OS declares the connection dead
+tcp_user_timeout_ms = 5000 # Unacked-data timeout (ms); keep below connection_timeout
# Redis credentials
[redis]
diff --git a/crates/common_utils/src/lib.rs b/crates/common_utils/src/lib.rs
index 2a05899d8b..e250a26f70 100644
--- a/crates/common_utils/src/lib.rs
+++ b/crates/common_utils/src/lib.rs
@@ -746,6 +746,22 @@ pub fn generate_time_ordered_id_without_prefix() -> String {
pub fn generate_id_with_len(length: usize) -> String {
nanoid::nanoid!(length, &consts::ALPHABETS)
}
+/// OS default is ~2 hours, far too slow to notice a connection killed by a managed-Postgres
+/// (Aurora/RDS/AlloyDB) failover.
+pub const DEFAULT_DB_KEEPALIVES_IDLE_SECS: u64 = 5;
+
+/// OS default is ~75s, which would make detection minutes-slow.
+pub const DEFAULT_DB_KEEPALIVES_INTERVAL_SECS: u64 = 2;
+
+/// With the two values above, a dead idle connection is detected in ~11s instead of hanging
+/// indefinitely.
+pub const DEFAULT_DB_KEEPALIVES_COUNT: u32 = 3;
+
+/// Bounds how long a checkout's health-check ping can hang against a peer that went silent
+/// without a TCP RST/FIN -- what managed-Postgres failover does. Must stay below
+/// `connection_timeout` (10s), or a hung ping masks the dead connection as healthy.
+pub const DEFAULT_DB_TCP_USER_TIMEOUT_MS: u64 = 5_000;
+
#[allow(missing_docs)]
pub trait DbConnectionParams {
fn get_username(&self) -> &str;
@@ -753,9 +769,27 @@ pub trait DbConnectionParams {
fn get_host(&self) -> &str;
fn get_port(&self) -> u16;
fn get_dbname(&self) -> &str;
+
+ /// See [`DEFAULT_DB_KEEPALIVES_IDLE_SECS`].
+ fn get_pool_keepalives_idle(&self) -> u64 {
+ DEFAULT_DB_KEEPALIVES_IDLE_SECS
+ }
+ /// See [`DEFAULT_DB_KEEPALIVES_INTERVAL_SECS`].
+ fn get_pool_keepalives_interval(&self) -> u64 {
+ DEFAULT_DB_KEEPALIVES_INTERVAL_SECS
+ }
+ /// See [`DEFAULT_DB_KEEPALIVES_COUNT`].
+ fn get_pool_keepalives_count(&self) -> u32 {
+ DEFAULT_DB_KEEPALIVES_COUNT
+ }
+ /// See [`DEFAULT_DB_TCP_USER_TIMEOUT_MS`].
+ fn get_pool_tcp_user_timeout_ms(&self) -> u64 {
+ DEFAULT_DB_TCP_USER_TIMEOUT_MS
+ }
+
fn get_database_url(&self, schema: &str) -> String {
format!(
- "postgres://{}:{}@{}:{}/{}?application_name={}&options=-c%20search_path%3D{}",
+ "postgres://{}:{}@{}:{}/{}?application_name={}&options=-c%20search_path%3D{}&keepalives=1&keepalives_idle={}&keepalives_interval={}&keepalives_count={}&tcp_user_timeout={}",
self.get_username(),
self.get_password().peek(),
self.get_host(),
@@ -763,6 +797,10 @@ pub trait DbConnectionParams {
self.get_dbname(),
schema,
schema,
+ self.get_pool_keepalives_idle(),
+ self.get_pool_keepalives_interval(),
+ self.get_pool_keepalives_count(),
+ self.get_pool_tcp_user_timeout_ms(),
)
}
}
diff --git a/crates/drainer/src/settings.rs b/crates/drainer/src/settings.rs
index a65202325c..331d419f8a 100644
--- a/crates/drainer/src/settings.rs
+++ b/crates/drainer/src/settings.rs
@@ -86,6 +86,10 @@ pub struct Database {
pub dbname: String,
pub pool_size: u32,
pub connection_timeout: u64,
+ pub keepalives_idle: u64,
+ pub keepalives_interval: u64,
+ pub keepalives_count: u32,
+ pub tcp_user_timeout_ms: u64,
}
impl DbConnectionParams for Database {
@@ -104,6 +108,18 @@ impl DbConnectionParams for Database {
fn get_dbname(&self) -> &str {
&self.dbname
}
+ fn get_pool_keepalives_idle(&self) -> u64 {
+ self.keepalives_idle
+ }
+ fn get_pool_keepalives_interval(&self) -> u64 {
+ self.keepalives_interval
+ }
+ fn get_pool_keepalives_count(&self) -> u32 {
+ self.keepalives_count
+ }
+ fn get_pool_tcp_user_timeout_ms(&self) -> u64 {
+ self.tcp_user_timeout_ms
+ }
}
#[derive(Debug, Clone, Deserialize)]
@@ -212,6 +228,10 @@ impl Default for Database {
dbname: String::new(),
pool_size: 5,
connection_timeout: 10,
+ keepalives_idle: common_utils::DEFAULT_DB_KEEPALIVES_IDLE_SECS,
+ keepalives_interval: common_utils::DEFAULT_DB_KEEPALIVES_INTERVAL_SECS,
+ keepalives_count: common_utils::DEFAULT_DB_KEEPALIVES_COUNT,
+ tcp_user_timeout_ms: common_utils::DEFAULT_DB_TCP_USER_TIMEOUT_MS,
}
}
}
diff --git a/crates/router/src/configs/defaults.rs b/crates/router/src/configs/defaults.rs
index 8de2f19b47..2089c7e593 100644
--- a/crates/router/src/configs/defaults.rs
+++ b/crates/router/src/configs/defaults.rs
@@ -48,6 +48,10 @@ impl Default for super::settings::Database {
min_idle_pool_size: 2,
max_lifetime: 1800,
idle_timeout: 300,
+ keepalives_idle: common_utils::DEFAULT_DB_KEEPALIVES_IDLE_SECS,
+ keepalives_interval: common_utils::DEFAULT_DB_KEEPALIVES_INTERVAL_SECS,
+ keepalives_count: common_utils::DEFAULT_DB_KEEPALIVES_COUNT,
+ tcp_user_timeout_ms: common_utils::DEFAULT_DB_TCP_USER_TIMEOUT_MS,
}
}
}
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index 5aff4c24d8..26263a2856 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -1204,6 +1204,10 @@ pub struct Database {
pub min_idle_pool_size: u32,
pub max_lifetime: u64,
pub idle_timeout: u64,
+ pub keepalives_idle: u64,
+ pub keepalives_interval: u64,
+ pub keepalives_count: u32,
+ pub tcp_user_timeout_ms: u64,
}
impl From<Database> for storage_impl::config::Database {
@@ -1220,6 +1224,10 @@ impl From<Database> for storage_impl::config::Database {
min_idle_pool_size: val.min_idle_pool_size,
max_lifetime: val.max_lifetime,
idle_timeout: val.idle_timeout,
+ keepalives_idle: val.keepalives_idle,
+ keepalives_interval: val.keepalives_interval,
+ keepalives_count: val.keepalives_count,
+ tcp_user_timeout_ms: val.tcp_user_timeout_ms,
}
}
}
diff --git a/crates/storage_impl/src/config.rs b/crates/storage_impl/src/config.rs
index 8ad3880eb7..43a65df15a 100644
--- a/crates/storage_impl/src/config.rs
+++ b/crates/storage_impl/src/config.rs
@@ -21,6 +21,14 @@ pub struct Database {
pub max_lifetime: u64,
#[serde(default = "default_idle_timeout")]
pub idle_timeout: u64,
+ #[serde(default = "default_keepalives_idle")]
+ pub keepalives_idle: u64,
+ #[serde(default = "default_keepalives_interval")]
+ pub keepalives_interval: u64,
+ #[serde(default = "default_keepalives_count")]
+ pub keepalives_count: u32,
+ #[serde(default = "default_tcp_user_timeout_ms")]
+ pub tcp_user_timeout_ms: u64,
}
const fn default_min_idle_pool_size() -> u32 {
@@ -35,6 +43,22 @@ const fn default_idle_timeout() -> u64 {
300
}
+const fn default_keepalives_idle() -> u64 {
+ common_utils::DEFAULT_DB_KEEPALIVES_IDLE_SECS
+}
+
+const fn default_keepalives_interval() -> u64 {
+ common_utils::DEFAULT_DB_KEEPALIVES_INTERVAL_SECS
+}
+
+const fn default_keepalives_count() -> u32 {
+ common_utils::DEFAULT_DB_KEEPALIVES_COUNT
+}
+
+const fn default_tcp_user_timeout_ms() -> u64 {
+ common_utils::DEFAULT_DB_TCP_USER_TIMEOUT_MS
+}
+
impl DbConnectionParams for Database {
fn get_username(&self) -> &str {
&self.username
@@ -51,6 +75,18 @@ impl DbConnectionParams for Database {
fn get_dbname(&self) -> &str {
&self.dbname
}
+ fn get_pool_keepalives_idle(&self) -> u64 {
+ self.keepalives_idle
+ }
+ fn get_pool_keepalives_interval(&self) -> u64 {
+ self.keepalives_interval
+ }
+ fn get_pool_keepalives_count(&self) -> u32 {
+ self.keepalives_count
+ }
+ fn get_pool_tcp_user_timeout_ms(&self) -> u64 {
+ self.tcp_user_timeout_ms
+ }
}
#[derive(Debug, serde::Deserialize, Clone, Copy, Default)]
@@ -84,6 +120,10 @@ impl Default for Database {
min_idle_pool_size: default_min_idle_pool_size(),
max_lifetime: default_max_lifetime(),
idle_timeout: default_idle_timeout(),
+ keepalives_idle: default_keepalives_idle(),
+ keepalives_interval: default_keepalives_interval(),
+ keepalives_count: default_keepalives_count(),
+ tcp_user_timeout_ms: default_tcp_user_timeout_ms(),
}
}
}